[英]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 我将此帖子用作参考: 流星:带有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 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 on server 服务器上的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();
});
Thank you for any input! 谢谢您的投入!
You can add reywood:publish-composite package. 您可以添加reywood:publish-composite软件包。 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). 此代码返回来自两个集合的数据:Roman和Avtor(我知道代码很奇怪)。
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
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.