简体   繁体   中英

Saved an object's id in a Meteor.user field, recalled the id in a template, how can i display the object's data instead of the id?

I have a collection of objects called Categories in a list. in my html, each category object is a list item and this category object has other objects in it, these are the category id, name and an array of posts that belong to that category. see image.

在此处输入图片说明

On each Category list item is a button that the user can click, this then saves the category id in a Meteor.user field called name.

<template name="CategoriesMain">
<ul>
  {{#each articles}}
    <li>
      <a href="/catsingle/CategorySingle/{{_id}}"><h2>{{name}}</h2></a>   
      <button type="button" class="toggle-category">Add to User Field</button>
    </li>
  {{/each}}
</ul>
</template>

to achieve this i have this in my helper

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
      //take the category id on click
          var ob = this._id;
          //make it into array
          var id = $.makeArray( ob );
          e.preventDefault();
          Meteor.call('addingCategory', id, function(error, user){ console.log(id)});
      },
});

then in my methods.

Meteor.methods({
    addingCategory: function(name) {
        Meteor.users.update({
      _id: Meteor.userId()
    },
    {
      $addToSet: {

        name: name
      }
    });
    }
});

如您所见,类别ID作为数组输入到用户数据中

Each user has a timeline in which the saved category ids appear.

<template name="userTimeline">
  {{#if currentUser}}
  <div class="timeline-user">
 <p>{{name}}</p>
  </div>
{{/if}}
</template>

在此处输入图片说明

in the helper

Template.userTimeline.helpers({

  name: function() {
    return Meteor.user().name;

//this is what im experiment with to link the category id's in the  timeline with the category ids from the Category collection but i'm not sure if im going the right direction.
    var name = FlowRouter.getParam('_id')
   return CategoryCollection.find({_id: id}).fetch();
  }
});

My question is, instead of displaying the category ids, can somehow get the objects of those category id and ie posts that belong in that category? i know i have to somehow link the id collected by the user with that of the category

EDIT

I have amended my meteor methods to declare "category" as an array like so:

Accounts.onCreateUser(function(options, user){
    user.category = [];
    return user;
});


Meteor.methods({
    addingCategory: function(category) {
        console.log(Meteor.userId());
        Meteor.users.update({
      _id: Meteor.userId()
    },
    {
      $addToSet: {
        category: category
      }
    });
    }
});

this is what the Meteor.users looks like, take a look at the "category" field.

在此处输入图片说明

I have amended the following in my helper, which is throwing the error:

Template.userTimeline.helpers({

  category(){
    return CategoryCollection.find({ _id: { $in: this.category }}); // a cursor of categories
  }
});

in which CategoryCollection is the collection holding the categories.

since i declared "category" as an array in my methods, i'm no longer changing each category id into an array as i was doing before, so i changed my template event to this.

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
          var ob = this._id;
          console.log(ob);
          e.preventDefault();
          Meteor.call('addingCategory', ob, function(error, user){ console.log(ob)});
      }
});

my Publish has changed to this: i don't know whether here i should use $in ?

Meteor.publish(null, function() {
  return Meteor.users.find({_id: this.userId}, {fields: {category: 1}});
});

my html has been changed to this:

<template name="userTimeline">
  {{#if currentUser}}
  <div class="timeline-user">
  {{#each category}}
    Count: {{count}}
    Description: {{description}}
    Link: {{link}}
    {{#each posts}}
      ID: {{id}}
      Title: {{title}}
    {{/each}}
  {{/each}}
  </div>
{{/if}}
</template>

You can show the categories related to a user by iterating over the array of category ids and using a helper to return the entire category object. Then inside that you can loop over the posts.

<template name="userTimeline">
  {{#if currentUser}}
  <div class="timeline-user">
  {{#each categories}}
    Count: {{count}}
    Description: {{description}}
    Link: {{link}}
    {{#each posts}}
      ID: {{id}}
      Title" {{title}}
    {{/each}}
  {{/each}
  </div>
{{/if}}
</template>

Then your helper:

template.userTimeline.helpers({
  categories(){
    if ( this.category && 
         typeof(this.category) === "object" &&
         this.category.length ){ // check to see if the list of categories is an array
      return categories.find({ _id: { $in: this.category }}); // a cursor of categories
    } else {
      console.log(this.category);
      return null;
    }
  }
});

Remember that the data context this for the userTimeline template is the Meteor.user() object so this.name is going to be the array of category ids in Meteor.user().name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM