【问题标题】:Meteor user profile page with data from another collectionMeteor 用户个人资料页面,其中包含来自另一个集合的数据
【发布时间】:2015-08-19 15:41:24
【问题描述】:

我有每个卖家的个人资料页面,可以私下和公开访问。我已发布卖家(用户)信息并向客户发送数据,但我正在努力将卖家的产品集合发送给客户。

如果我可以将个人资料页面的用户信息发送到 Products 集合,我可以为特定卖家发布产品,但我目前陷入困境。我用这篇文章作为参考:Meteor: User Profile Page with Iron Router

这是我迄今为止所拥有的,它不是 DRY:

客户端模板

<template name="profile">
<div style="margin-top 5em;">

    <h1>Profile.Name: {{profile.name}}</h1>
    <h1>Username: {{username}}</h1>
    <h1>Profile.Description: {{profile.description}}</h1>

    {{#each products}}

        <h1>Products! {{username}}</h1>
        <h1>price {{price}}</h1>

    {{/each}}
</div>
</template>

客户端助手

Meteor.subscribe("userProfile", this.params.username);

Template.profile.helpers({
  products : function(){
    return Products.find();
  }
});

lib 上的 router.js

Router.route("/artist/:username", {
    name:"profile",
    waitOn:function(){
        var username=this.params.username;
        return Meteor.subscribe("userProfile", username);
        return Meteor.subscribe("products-by-id", username);
    },
    data:function(){
        var username=this.params.username;
        return Meteor.users.findOne({username:username});
        return Meteor.subscribe("products-by-id", username);
      },

  fastRender: true
});

服务器上的publications.js

Meteor.publish("userProfile",function(username){
    // simulate network latency by sleeping 2s
    Meteor._sleepForMs(2000);
    var user=Meteor.users.findOne({
        username:username
    });
    if(!user){
        this.ready();
        return;
    }
    if(this.userId==user._id){
    }
    else{
        return Meteor.users.find(user._id,{
            fields:{
                "profile.name": 1,
                "profile.description" : 1,
                "_id" : 1,
                "username" : 1,
                "profile.username" : 1
            }
        });
        return Products.find({username: user.username});
    }
});

Meteor.publish("allProducts", function(){
    return Products.find();
});

感谢您的任何意见!

【问题讨论】:

  • 首先你在一个块中使用了太多的回报。你知道只有第一个会被执行。也可以尝试查看dburles:collection-helpers,但很难给出一个好的答案,因为我几乎不明白你的问题。
  • 谢谢你,这肯定会让我更接近。基本上我有用户,他们有产品要卖。对于每个用户,在他们的个人资料页面上,我想显示他们出售的产品。您指向的氛围收藏有一个作者和书籍收藏——在作者的个人资料页面上获取一位作者的所有书籍是我要解决的同一个问题。

标签: javascript meteor


【解决方案1】:

您可以添加reywood:publish-composite 包。这个包允许像连接一样的“链接”集合。

Meteor.publishComposite('AutorTexts', function(avatar) {
 check(avatar, Match.Any);
 return {
  find: function(autorId) {
   check(autorId, Match.Any)
   return Roman.find({
    autor_id: avatar
   });
  },
  children: [{
   find: function(avtor) {
    return Avtor.find({
     _id: avatar
    });
   }
  }]
 };

});

此代码从两个集合返回数据:Roman 和 Avtor(我知道代码很奇怪)。

你还需要配置iron-router订阅路由:

Router.route('/a/:_id', function() {
  //console.log(this.params._id);
  this.render('AvtorAll');
  SEO.set({
    title: 'blahblah title'
  });
}, {
  path: '/a/:_id',
  // data: {id: this.params._id},
  name: 'AvtorAll',
  waitOn: function() {
    return Meteor.subscribe('AutorTexts', this.params._id);
  },
    onAfterAction: function() {
    if (!Meteor.isClient) { // executed only on client side!!
      return;
    }
    SEO.set({
      title: 'blahblah : ' + Avtor.findOne().autor_name,
      og: {
        "title": "blahblah : "  + Avtor.findOne().autor_name,
        "description": "blahblah . Powered by MeteorJS",
        "url": Router.current().route.path(this),
        "site_name": "blahblah ",
        "locale": "you_locale_here" // didnt work
      }
    });
  }

});

【讨论】:

  • 这看起来正是我所需要的。我会试试看,让你知道,谢谢!
猜你喜欢
  • 2012-01-24
  • 2014-11-22
  • 2016-05-13
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 1970-01-01
相关资源
最近更新 更多