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