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

enter image description here

I have a second collection called posts which is a collection of posts, and these posts have an id. take a look at the second image

enter image description here

I want to create a many to many relationships between the posts and the reblog collection. i.e, I want to match the integer

descovered: 9

from the reblog collection, with:

id: 9

from the posts collection so that I can display only the posts matched from the reblog collection. This of course will allow me to display the title of the post and other attributes.

This is my 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

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

There are 2 best solutions below

0
On

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. 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
}
});
2
On

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. Also since you are using Blaze you can just return a cursor instead of an array. I suspect you also meant to use your FlowRouter _id parameter in your first .find(). 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 } });
  }
);