简体   繁体   中英

Meteor user profile page with data from another collection

I have a profile page for each seller that is privately and publicly available. I have the seller (user) information published and sending data to the client but I'm struggling with sending that seller's product collection to the client.

If I could send the user information for the profile page to the Products collection I could publish the products for the specific seller, but I'm currently stuck. I used this post as a reference: Meteor: User Profile Page with Iron Router

This is what I have thus far, it's not DRY:

template on client

<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>

helpers on client

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

publications.js on server

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

Thank you for any input!

You can add reywood:publish-composite package. This package allow "link" collecttion like joins.

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

});

this code returns data from two collections: Roman & Avtor (code is weird, i know).

Also you need configure iron-router subscribe on route:

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM