[英]How to match an integer from one collection with it's identical in another collection, creating a many to many relationship.
This is supposed to be a simple many to many relationships in meteor but I must be missing something because i cannot get it to work. 在流星中这应该是简单的多对多关系,但是我必须丢失一些东西,因为我无法使它正常工作。
I have a Collection called reblog and in it is an array of integers called descovered
see image 我叫一个reblog集合,它是所谓的整数数组descovered
看到图像
I have a second collection called posts which is a collection of posts, and these posts have an id. 我有另一个名为posts的集合,它是一个帖子集合,这些帖子有一个ID。 take a look at the second image 看第二张图片
I want to create a many to many relationships between the posts and the reblog collection. 我想在帖子和reblog集合之间创建多对多关系。 ie, I want to match the integer 即,我想匹配整数
descovered: 9
from the reblog collection, with: 来自reblog集合,具有:
id: 9
from the posts collection so that I can display only the posts matched from the reblog collection. 来自帖子集合,因此我只能显示与reblog集合匹配的帖子。 This of course will allow me to display the title of the post and other attributes. 当然,这将允许我显示帖子的标题和其他属性。
This is my js 这是我的js
Template.reblogging.helpers({
descovered() {
var id = FlowRouter.getParam('_id');
//fetch the reblog collection contents
var rebloged = reblog.find().fetch();
//log below is showing that the fetch is successful because i can see the objects fetched in console
console.log(rebloged);
//create the relationship between the posts collection and the reblog collection
var reblogger = posts.find({
id: {
$in: rebloged
}
}).fetch();
//nothing is showing with the log below, so something is going wrong with the line above?
console.log(reblogger);
return reblogger
}
});
I must be missing something because this seems a pretty straightforward thing but it's not woring 我一定想念一些东西,因为这看起来很简单,但并不令人担忧
And my HTML is like this 我的HTML就是这样
<template name="reblogging">
{{#each descovered }}
<ul class="">
<li>
<h5 class="">{{title.rendered}}</h5>
</li>
</ul>
{{/each}}
</template>
You don't need to convert to strings and parse, you can use .map()
directly on the cursor to create an array of descovered
values. 你并不需要转换为字符串和解析,您可以使用.map()
直接在光标创建数组descovered
值。 Also since you are using Blaze you can just return a cursor instead of an array. 另外,由于您使用的是Blaze,因此您可以仅返回游标而不是数组。 I suspect you also meant to use your FlowRouter _id
parameter in your first .find()
. 我怀疑您还打算在第一个.find()
使用FlowRouter _id
参数。 If you didn't then there's no need to get that param in your helper. 如果您不这样做,则无需在助手中获取该参数。
Template.reblogging.helpers({
descovered() {
const id = FlowRouter.getParam('_id');
const reblogArr = reblog.find(id).map(el => { return el.descovered });
return posts.find({ id: { $in: reblogArr } });
}
);
As it turns out, the matching was accurate, however, the data from reblog
collection needed to be treated with REGEX to get rid of everything else apart from the values I needed, then turn them into an array, this is the final code that worked. 事实证明,匹配是准确的,但是,需要用REGEX处理来自reblog
收集的数据,以除去除我需要的值以外的所有其他内容,然后将它们转换为数组,这是最终的代码。 leaving it here, Hopefully, it will help someone in the future. 希望把它留在这里,将来会对某人有所帮助。
Template.reblogging.helpers({
descovered() {
var id = FlowRouter.getParam('_id');
//fetch the reblog collection contents
var rebloged = reblog.find().fetch();
//log below is showing that the fetch is successful because i can see the objects fetched in console
console.log(rebloged);
//turn it into a string so i can extract only the ids
var reblogString = JSON.stringify(rebloged).replace(/"(.*?)"/g, '').replace(/:/g, '').replace(/{/g, '').replace(/}/g, '').replace(/,,/g, ',').replace(/^\[,+/g, '').replace(/\]+$/g, '');
//after have extracted what i needed, i make it into an array
var reblogArr = reblogString.split(',').map(function(item) {
return parseInt(item, 10);
});
//create the relationship between the posts collection and the reblog collection
var reblogger = posts.find({
id: {
$in: reblogArr
}
}).fetch();
//nothing is showing with the log below, so something is going wrong with the line above?
console.log(reblogger);
return reblogger
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.