流星用户配置文件页面,其中包含来自另一个集合的数据
Meteor user profile page with data from another collection
我有一个个人资料页面,每个卖家是私人和公开可用的。我有卖方(用户)信息发布和发送数据给客户端,但我正在努力发送该卖家的产品集合给客户端。
如果我可以将配置文件页面的用户信息发送到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
}
});
}
});
相关文章:
- Meteor:访问另一个集合,每个块中有一个id
- 另一个集合视图中的集合视图
- Mongo-Aggregate:如何与另一个集合中的字段进行比较
- Meteor - 将 mongodb 查询分配给变量并将其插入到另一个集合
- 基于 MongoDB 中另一个集合中的数据查询一个集合中的数据
- 流星 - 将数据从一个集合移动到另一个集合
- 如何在插入时引用另一个集合
- 主干:将模型从一个集合添加到另一个集合
- 用于将树分支从集合移动到另一个集合的Firebase API
- 在Meteor(MongoDB)中,按另一个集合中的值搜索一个集合
- Meteor从数组返回值,以便在另一个集合中使用它
- Restangular:将元素从一个集合插入到具有不同路线的另一个集合
- 在backbone.js中将一个集合添加到另一个集合中
- 在另一个集合中存储引用会创建对象,但不会创建字符串Meteor
- 当发布的游标基于另一个集合内容时,如何构建响应式发布
- 更新来自另一个集合的匹配记录
- 如何在另一个集合中插入具有字段引用文档的文档
- mongodb中另一个集合的增量字段
- 流星用户配置文件页面,其中包含来自另一个集合的数据
- MongoDB:从另一个集合推送相关数据