javascript - 如何让 Meteor 的子项发布Composite
问题描述
我坚持使用 Meteor 的 publishComposite。我不明白在 publishComposite 中定义子集合的正确方法是什么。
假设我有这样的接口
interface IInterview {
_id: string,
candidateId: string,
comments: IInterviewComments[]
}
interface IInterviewComment {
userId: string,
text: string
}
interface IUser {
_id: string,
name: string
}
因此,根据一些采访的数据,我还需要评论作者的姓名
我写了这样的东西
publishComposite('interviews.item', function(interviewId) {
return {
find() {
return InterviewCollection.find({_id: interviewId});
},
children: [
find(interview) {
const authorsIds = interview.comments.map((comment) => comment.userId);
return UsersCollection.find({_id: {$in: authorsIds}});
}
]
}
});
那么,我应该如何在客户端获取评论作者的姓名?我可以简单地执行UsersCollection.findOne({_id: commentUserId})
还是应该使用一些收集助手?
解决方案
publishComposite 仅将所有相关数据发布到客户端对应的集合下。然后,您可以像往常一样自由地访问它,或者按照您认为合适的方式抽象它。您可以使用不同的方法进行抽象。您可能会查看的特定包是jagi:astronomy
、dburles:collection-helpers
或socialize:base-model
.
如果您想要将数据组合在一起的东西,以便在您接受采访时,采访的评论位于评论属性中,并且评论的作者在每条评论中都指定,那么我建议您查看cultofcoders:grapher
推荐阅读
- html - 如何使用带有滑块的 span 标签?
- r - 是否可以让 R 将一个数字放在一个列中,计算一个值的每个实例?
- rust - 如何在安全的 Rust 中为未调整大小的类型及其拥有的对应对象(如 `str` 和 `String`)创建新类型?
- python - 如何使用python将文件夹及其子文件夹和文件上传到谷歌驱动器文件夹中
- spring-boot - 在 oauth2 资源服务器中检索用户信息
- c++ - 如何通过取 n 行/列然后跳过 m 行/列来对 Eigen 中的矩阵进行切片
- python - 非凸函数优化
- python - 如果用户输入等于键,则获取字典值
- asp.net-core - SignalR OnConnected 找不到合适的方法来覆盖?
- html - 如何将不断增长的文本区域内联与按钮对齐