流星用户配置文件页面,其中包含来自另一个集合的数据

Meteor user profile page with data from another collection

本文关键字:另一个 集合 数据 包含 配置文件 用户 流星      更新时间:2023-09-26

我有一个个人资料页面,每个卖家是私人和公开可用的。我有卖方(用户)信息发布和发送数据给客户端,但我正在努力发送该卖家的产品集合给客户端。

如果我可以将配置文件页面的用户信息发送到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>

客户端helper

Meteor.subscribe("userProfile", this.params.username);
Template.profile.helpers({
  products : function(){
    return Products.find();
  }
});

router.js on lib

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

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

谢谢你的意见!

可以添加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
    });
   }
  }]
 };
});

这段代码从两个集合返回数据: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
      }
    });
  }
});