通过给定 ID 获取文档标题的“meteor”方式是什么?
收藏(文章)
{
'_id' : 'Dn59y87PGhkJXpaiZ',
'title' : 'Sample Article',
'slug' : 'sample-article'
}
客户端
render() {
const data = [
{ _id: 'Dn59y87PGhkJXpaiZ' },
{ _id: 'kJXpaiZDn59y87PGh' }
{ _id: 'y87PGhkJXpaiZDn59' }
]
return (
<List>
{
data.map(r => {
return <List.Item>r._id</List.Item>
})
}
)
}
这样我会得到这个输出:
<List>
<List.Item>Dn59y87PGhkJXpaiZ</List.Item>
<List.Item>kJXpaiZDn59y87PGh</List.Item>
<List.Item>y87PGhkJXpaiZDn59</List.Item>
</List>
现在我想显示标题而不是 id。所以通常我会这样做
data.map(r => {
const title = Articles.findOne({ _id: r._id }).title
return <List.Item>title</List.Item>
})
但问题是,data
是一个动态数据集,我不能/不想发布完整的文章集合。目前没有订阅,所以我没有得到该标题的任何结果。
所以我想我必须进行服务器端调用。
Meteor.call('getTitle', r._id, function(err, res) {
console.log(res)
})
但是我会在回调函数中得到结果。那么我如何将这些添加到列表中呢?我还想避免多次方法调用。我认为最好发送数据
并在服务器端获取所有标题,然后构建列表。
最佳答案
如果您可以/想要使用非异步调用,请不要将回调传递给 Meteor.call() 方法:
data.map(r => {
const title = Meteor.call('getTitle',r._id);
return <List.Item>title</List.Item>
})
如 docs 中所述:
If you do not pass a callback on the server, the method invocation will block until the method is complete. It will eventually return the return value of the method, or it will throw an exception if the method threw an exception.
关于javascript - meteor JS : How to get title data via server method by given id array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44687470/