Reactjs &引用;“链接”;流星订阅
我正在使用React和Reactjs &引用;“链接”;流星订阅,reactjs,meteor,meteor-react,Reactjs,Meteor,Meteor React,我正在使用React和createContainer。我正在寻找一种方法,可以把两个电话连在一起 例如,如果我有以下数据: // Category { _id: 'ABC', name: 'Cat 1' } // Item { catId: 'ABC', slug: 'slug' } 在我的createContainer中,我想通过它的slug(Items.find({slug}))获取项。然后,我想通过item.catId获取类别 我试过这样的方法,但没用: cr
createContainer
。我正在寻找一种方法,可以把两个电话连在一起
例如,如果我有以下数据:
// Category
{
_id: 'ABC',
name: 'Cat 1'
}
// Item
{
catId: 'ABC',
slug: 'slug'
}
在我的createContainer
中,我想通过它的slug(Items.find({slug})
)获取项。然后,我想通过item.catId
获取类别
我试过这样的方法,但没用:
createContainer(({ slug }) => {
const itemHandler = Meteor.subscribe('Item.bySlug', slug);
const item = Items.findOne();
const categoryHandler = Meteor.subscribe('Category.byId', (item.id || {}).id); // also tried just item.id and got undefined for item
const category = Categories.findOne();
return { item, category };
}, Component);
我可以很好地获得项目
,但在类别
上没有骰子,它仍然没有定义。我肯定我没有反应性地触发某些东西,但我不太确定在这种情况下正确的模式是什么,或者是否有更精简的方式。您实际上是在进行客户端连接。在这些情况下,我通常进行服务器端连接。在Item.bySlug publisher中,我将执行以下操作:
let itemCursor = Items.find({slug: slug});
let transformItem = (fields) => {
// making an assumption here that Item has a categoryId that indicates its category
let category = Categories.findOne(fields.categoryId);
// attach the category to the Item
fields.category = category;
return fields;
};
let handle = itemCursor.observeChanges({
added: (id, fields) => {
fields = transformItem(fields);
this.added('items', id, fields);
},
changed: (id, fields) => {
fields = transformItem(fields);
this.changed('items', id, fields);
},
removed: (id) => {
this.removed('items', id);
}
});
this.ready();
this.onStop(() => {
handle.stop();
});
Meteor.publish("your.publication.name", function(slug, id){
let itemCursor = Items.find(your_selector);
let categoryCursor = Categories.find(your_selector);
return [itemCursor, categoryCursor];
});
现在,在客户端上,您只需订阅所需的项目,其类别将被附加。您实际上是在进行客户端连接。在这些情况下,我通常进行服务器端连接。在Item.bySlug publisher中,我将执行以下操作:
let itemCursor = Items.find({slug: slug});
let transformItem = (fields) => {
// making an assumption here that Item has a categoryId that indicates its category
let category = Categories.findOne(fields.categoryId);
// attach the category to the Item
fields.category = category;
return fields;
};
let handle = itemCursor.observeChanges({
added: (id, fields) => {
fields = transformItem(fields);
this.added('items', id, fields);
},
changed: (id, fields) => {
fields = transformItem(fields);
this.changed('items', id, fields);
},
removed: (id) => {
this.removed('items', id);
}
});
this.ready();
this.onStop(() => {
handle.stop();
});
Meteor.publish("your.publication.name", function(slug, id){
let itemCursor = Items.find(your_selector);
let categoryCursor = Categories.find(your_selector);
return [itemCursor, categoryCursor];
});
现在,在客户端上,您只需订阅所需的项目,其类别将被附加。解决此问题的最简单方法是从服务器端发布
返回游标的数组,然后调用集合.findOne()订阅后的客户端中的
发布代码如下所示:
let itemCursor = Items.find({slug: slug});
let transformItem = (fields) => {
// making an assumption here that Item has a categoryId that indicates its category
let category = Categories.findOne(fields.categoryId);
// attach the category to the Item
fields.category = category;
return fields;
};
let handle = itemCursor.observeChanges({
added: (id, fields) => {
fields = transformItem(fields);
this.added('items', id, fields);
},
changed: (id, fields) => {
fields = transformItem(fields);
this.changed('items', id, fields);
},
removed: (id) => {
this.removed('items', id);
}
});
this.ready();
this.onStop(() => {
handle.stop();
});
Meteor.publish("your.publication.name", function(slug, id){
let itemCursor = Items.find(your_selector);
let categoryCursor = Categories.find(your_selector);
return [itemCursor, categoryCursor];
});
现在,您将从客户端的Item和Category集合中获得必要的文档 解决问题的最简单方法是从服务器端发布返回游标的数组,并在订阅后在客户端调用集合.findOne()
发布代码如下所示:
let itemCursor = Items.find({slug: slug});
let transformItem = (fields) => {
// making an assumption here that Item has a categoryId that indicates its category
let category = Categories.findOne(fields.categoryId);
// attach the category to the Item
fields.category = category;
return fields;
};
let handle = itemCursor.observeChanges({
added: (id, fields) => {
fields = transformItem(fields);
this.added('items', id, fields);
},
changed: (id, fields) => {
fields = transformItem(fields);
this.changed('items', id, fields);
},
removed: (id) => {
this.removed('items', id);
}
});
this.ready();
this.onStop(() => {
handle.stop();
});
Meteor.publish("your.publication.name", function(slug, id){
let itemCursor = Items.find(your_selector);
let categoryCursor = Categories.find(your_selector);
return [itemCursor, categoryCursor];
});
现在,您将从客户端的Item和Category集合中获得必要的文档 事实证明,我发布的代码确实有效,我只是遇到了一个数据问题,Items.categoryId
没有正确填充
在这个特定的例子中,我确实想做一个客户端连接,我所做的工作确实有效。项目位是react,一旦加载,它将实际重新运行,然后正确填充该字段
createContainer(({ slug }) => {
const itemHandler = Meteor.subscribe('Item.bySlug', slug);
const item = Items.findOne();
const categoryHandler = Meteor.subscribe('Category.byId', (item.id || {}).id); // also tried just item.id and got undefined for item
const category = Categories.findOne();
return { item, category };
}, Component);
第一次运行时,项
将被定义,类别
将为空。下一步操作(只要项目
准备就绪)将填充类别
。需要两次跳跃,但效果很好。事实证明,我发布的代码确实有效,我只是遇到了一个数据问题,其中项。categoryId
没有正确填充
在这个特定的例子中,我确实想做一个客户端连接,我所做的工作确实有效。项目位是react,一旦加载,它将实际重新运行,然后正确填充该字段
createContainer(({ slug }) => {
const itemHandler = Meteor.subscribe('Item.bySlug', slug);
const item = Items.findOne();
const categoryHandler = Meteor.subscribe('Category.byId', (item.id || {}).id); // also tried just item.id and got undefined for item
const category = Categories.findOne();
return { item, category };
}, Component);
第一次运行时,项
将被定义,类别
将为空。下一步操作(只要项目
准备就绪)将填充类别
。需要两次跳跃,但效果很好。此软件包对您不起作用吗?这个包裹不适合你吗?这是从一个集合发布单个文档和从另一个集合发布相关文档时解决此特定问题的最简单解决方案。这是从一个集合发布单个文档和从另一个集合发布相关文档时解决此特定问题的最简单解决方案。