簡體   English   中英

流星用戶個人資料頁面,其中包含來自另一個集合的數據

[英]Meteor user profile page with data from another collection

我為每個公開和不公開的賣家提供了個人資料頁面。 我已經發布了賣方(用戶)信息並將數據發送給客戶,但是我正努力將賣方的產品集發送給客戶。

如果我可以將個人資料頁面的用戶信息發送到“產品”集合,則可以為特定賣家發布產品,但是我目前處於困境。 我將此帖子用作參考: 流星:帶有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();
});

謝謝您的投入!

您可以添加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(我知道代碼很奇怪)。

另外,您還需要配置鐵路由器在路由上訂閱:

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

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM