简体   繁体   English

如何将一个集合中的整数与另一个集合中的整数匹配,从而创建多对多关系。

[英]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.

相关问题 Sequelize - 一对多关系延迟加载并返回一个实例而不是一个集合 - Sequelize - One to Many relationship lazy load and return one instance and not a collection sails.js一对多关系-变量所有者集合属性 - sails.js one to many relationship - variable owner collection attribute 如何将数据与 React Native 和多对多 Firebase Firestore 集合关系连接起来? - How can I join data with React native and a many to many firebase firestore collection relationship? Meteor,一对多关系并仅在发布中向客户端集合添加字段? - Meteor, One to Many Relationship & add field only to client side collection in Publish? 收集作为EmberJS中的属性? (一对多关系) - Collection as an attribute in EmberJS? (One-to-many relation) 如何使用一个集合中的文档字段从不同集合中检索另一个文档字段? - How do I use a document's field from one collection to retrieve another document field from a different collection? 许多原型为收集类型 - Many prototype for collection type 如何将另一个集合中的数据添加到检索到的 Firestore onSnapshot 集合(如外键关系)? - How to add data from another collection to a retrieved Firestore onSnapshot collection (like foreign key relationship)? 如何使用 firebase 更新集合中的许多记录? - How to update many records in a collection using firebase? 将 ObjectId 作为数组从一个集合引用和插入到另一个集合 - Referencing and Inserting ObjectId's from one collection to another as an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM