【问题标题】:Meteor: How can i push items to users collections and create a list or array instead of replacing each item with the new one?Meteor:如何将项目推送到用户集合并创建列表或数组,而不是用新项目替换每个项目?
【发布时间】:2016-07-16 22:05:34
【问题描述】:

我正在尝试通过单击事件将来自另一个集合的对象附加到 Meteor.user 集合。我有一个集合,其中包含一个名为“类别”的项目列表,每个类别都有一个名称字段,我想将它的名称推送到 meteor.user 中。

它的工作方式应该是用户可以根据需要推送任意数量的这些名称,但是它只接受一个条目,当我单击另一个名称时,新名称会替换旧名称,而不是大批。我怎样才能让它允许很多条目?

客户端/users.js

Template.CategoriesMain.events({
  'click .toggle-category': function(e){
          //var id = $(e.target).attr('posts.name');
          var id = $(e.target).parent().find("a").text();
          console.log(id);

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

服务器/users.js

Meteor.methods({
    addingCategory: function(name) {

      var cats = [{}];
      cats.push(name);
        console.log(Meteor.userId());
        Meteor.users.update({
      _id: Meteor.userId()
    }, {
      $set: {

        name: name
      }
    });
    }
});

这是来自 db.user.find() 的用户,正如您在

中看到的那样

“姓名”:“a-reece”

它显然在推动名称,但我不能添加更多,我只能替换

{ "_id" : "4CHcZjSD4hCrqweGA", "createdAt" : ISODate("2016-07-13T21:38:59.505Z"), "服务" : { "密码" : { “bcrypt”: “$2a$10$lKZtrYSMD4EGPj6eamgFDuPZ41Jw52DgivBly3lUYWbGDtfZBg1X。” }, “简历”:{“登录令牌”:[{“何时”: ISODate("2016-07-13T21:38:59.719Z"), "hashedToken" : "BsqTGedB2FkmSPO3+5I31rOM2+MPtF97Zc9tRQ4pf8Y=" } ] } }, "电子邮件" : [ { “地址”:“mun@les.com”,“已验证”:假}],“角色”:[ "discoveror", "yes" ], "isAdmin" : true, "name" : "a-reece" }

如何添加更多名称而不是替换?

编辑

Meteor.methods({
    addingCategory: function(name) {

        //Meteor.users.update(Meteor.userId(), { $addToSet: { name: name} } );
        console.log(Meteor.userId());
        //Meteor.users.update(Meteor.userId(), { $set: { "categories": cats }} );
        Meteor.users.update({
      _id: Meteor.userId()
    },
      {
        $unset: {
            name: name
        }
      },

    {
      $addToSet: {

        name: name
      }
    });
    }
});

回答

 Template.CategoriesMain.events({
  'click .toggle-category': function(e){
          //var id = $(e.target).attr('posts.name');
          var ob = $(e.target).parent().find("a").text();
          var id = $.makeArray( ob );
          console.log(id);

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

【问题讨论】:

    标签: javascript arrays json mongodb meteor


    【解决方案1】:

    你目前正在做的事情:

    Meteor.users.update({ _id: Meteor.userId() }, { $set: { name: name } });
    

    您有两个选择:$push 或 $addToSet:

    Meteor.users.update({ _id: Meteor.userId() }, { $push: { name: name } });
    

    或

    Meteor.users.update({ _id: Meteor.userId() }, { $addToSet: { name: name } });
    

    前者压入数组,允许重复,后者避免重复。

    你不需要:

    var cats = [{}];
    cats.push(name);
    

    【讨论】:

    • 感谢您回复@MichelFloyd。当我使用 £addToSet 时,我收到以下错误 $addToSet 到非数组字段。名为'name'的字段在文档_id中有一个非数组类型String:
    • 那是因为您之前已经将$set 字段作为字符串而不是数组。您必须先$unset 任何此类字段。
    • 你的意思是方法吗?请查看问题底部的我的编辑。我是这样做的,但是控制台和终端都没有错误,但没有向用户添加任何内容。请告知@MichelFloyd
    • 你不能在同一个更新语句中取消设置和addToSet。因为您已经在数据库中植入了非数组元素,所以您应该先将它们转换为数组(一次性操作),然后再使用 $addToSet 。
    • 好的,谢谢。我先把它做成一个数组,然后我做了 $addToset。有效。我感谢您的所有帮助@MichelFloyd
    猜你喜欢
    • 2015-03-28
    • 2017-07-10
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2018-10-15
    • 2021-07-06
    • 2021-09-09
    • 2019-10-30
    相关资源
    最近更新 更多