0

这应该是流星中简单的多对多关系,但我必须遗漏一些东西,因为我无法让它工作。

我有一个名为reblogdescovered的集合,其中有一个名为see image的整数数组

在此处输入图像描述

我有第二个集合,称为帖子,它是帖子的集合,这些帖子有一个 ID。看看第二张图片

在此处输入图像描述

我想在帖子转发集合之间创建多对多关系。即,我想匹配整数

descovered: 9

来自 reblog 集合,其中:

id: 9

帖子集合中,以便我只能显示与转发集合匹配的帖子。这当然可以让我显示帖子的标题和其他属性。

这是我的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
}
});

我一定遗漏了一些东西,因为这似乎是一件很简单的事情,但它没有用

我的HTML是这样的

<template name="reblogging">
 {{#each descovered }}
<ul class="">
  <li>
    <h5 class="">{{title.rendered}}</h5>
  </li>
</ul>
{{/each}}
</template>
4

2 回答 2

1

您不需要转换为字符串和解析,您可以.map()直接在光标上使用来创建一个descovered值数组。此外,由于您使用的是 Blaze,您可以只返回游标而不是数组。我怀疑您还打算_id在第一个.find(). 如果您不这样做,则无需在您的助手中获取该参数。

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 } });
  }
);
于 2017-05-16T18:31:24.650 回答
0

事实证明,匹配是准确的,但是,reblog需要使用 REGEX 处理来自集合的数据,以摆脱除我需要的值之外的所有其他内容,然后将它们转换为数组,这是最终有效的代码。把它留在这里,希望它会在未来对某人有所帮助。

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
}
});
于 2017-05-16T12:42:34.123 回答