Meteor用户个人资料页面,包含来自其他集合的数据

时间:2015-08-18 10:47:34

标签: javascript meteor

我有一个私人和公开的每个卖家的个人资料页面。我发布了卖家(用户)信息并将数据发送给客户,但我很难将该卖家的产品集合发送给客户。

如果我可以将个人资料页面的用户信息发送到产品系列,我可以为特定卖家发布产品,但我目前卡住了。我使用这篇文章作为参考:Meteor: User Profile Page with Iron Router

这是我迄今为止所做的,它不是干的:

客户端上的

模板

<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();
});

感谢您的任何意见!

1 个答案:

答案 0 :(得分:2)

您可以添加reywood:publish-composite个包裹。这个包允许&#34;链接&#34;收集就像加入一样。

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&amp; Avtor(代码很奇怪,我知道)。

此外,您还需要在路线上配置铁路由器订阅:

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
      }
    });
  }

});
相关问题