繁体   English   中英

将对象的ID保存在Meteor.user字段中,在模板中调用该ID,如何显示对象的数据而不是ID?

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

我在列表中有一个名为Categories的对象集合。 在我的html中,每个类别对象都是一个列表项,并且该类别对象中还有其他对象,这些是类别ID,名称和属于该类别的帖子数组。 见图片。

在此处输入图片说明

用户可以在每个类别列表项上单击一个按钮,然后将类别ID保存在名为name的Meteor.user字段中。

<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>

为了实现这一点,我在我的助手中有这个

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)});
      },
});

然后用我的方法。

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

        name: name
      }
    });
    }
});

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

每个用户都有一条时间表,其中显示了已保存的类别ID。

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

在此处输入图片说明

在帮手中

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();
  }
});

我的问题是,不显示类别ID,而是可以某种方式获取那些类别ID的对象,即属于该类别的帖子? 我知道我必须以某种方式将用户收集的ID与该类别的ID相关联

编辑

我修改了流星方法,将“ category”声明为数组,如下所示:

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
      }
    });
    }
});

这就是Meteor.users的外观,请看一下“类别”字段。

在此处输入图片说明

我已经在助手中修改了以下内容,这引发了错误:

Template.userTimeline.helpers({

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

其中CategoryCollection是保存类别的集合

由于我在方法中将“类别”声明为数组,因此不再像以前那样将每个类别ID更改为数组,因此我将模板事件更改为此。

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)});
      }
});

我的发布已更改为:我不知道在这里是否应该使用$ in?

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

我的html已更改为:

<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>

您可以通过遍历类别ID数组并使用帮助程序返回整个类别对象来显示与用户相关的类别。 然后,在其中可以循环查看帖子。

<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>

然后您的助手:

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;
    }
  }
});

请记住,数据上下文thisuserTimeline模板是Meteor.user()以便对象this.name将是类别ID的在阵列Meteor.user().name

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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