【问题标题】:Meteor.user custom field displaying a single list of items in one html tag instead of each item in its own tagMeteor.user 自定义字段在一个 html 标记中显示单个项目列表,而不是在其自己的标记中显示每个项目
【发布时间】:2016-07-24 09:38:11
【问题描述】:

我有一个自定义用户字段,用户在单击按钮时填充该字段,该按钮具有来自另一个集合的项目的 ID,但是当我返回它时,我会在一个 html 标记中获得一个项目列表,而不是将每个保存的项目返回到其自己的标签中,结果如下所示

就是这样

<p>CategoryPublication-98,CategoryPublication-2,CategoryPublication-57<p>

什么时候应该是这样的

    <p>CategoryPublication-98</p>
    <p>CategoryPublication-2</p>
    <p>CategoryPublication-57</p>

这是我的发布

Meteor.publish(null, function() {
  return Meteor.users.find({_id:{$in:fields.name}}).fetch();
});

我的html

<template name="userTimeline">

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

  {{/each}}
  </div>

{{/if}}
</template>

我的助手

Template.userTimeline.helpers({

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

我的插入

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
          var ob = this._id;
          var id = $.makeArray( ob );

          console.log(id);

          e.preventDefault();
          Meteor.call('addingCategory', id, function(error, user){ console.log(id)});
      },
});

我的方法

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

        name: name
      }
    });
    }
});

【问题讨论】:

    标签: javascript arrays meteor


    【解决方案1】:

    你在哪里:

    {{#each name}}
      <p>{{name}}</p>
    {{/each}}
    

    内部&lt;p&gt;&lt;/p&gt; 中name 的值存在歧义。由于您只是返回一个字符串数组而不是对象,因此数组中没有 name 键。

    改为:

    {{#each name}}
      <p>{{this}}</p>
    {{/each}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2016-04-02
      • 2014-06-23
      • 2013-08-31
      • 1970-01-01
      • 2018-06-26
      • 2020-07-25
      相关资源
      最近更新 更多