Shingai Munyuki
Shingai Munyuki

Reputation: 611

Meteor: How can i push items to users collections and create a list or array instead of replacing each item with the new one?

I'm trying to attach objects from another collection to the Meteor.user collection by a click event. I have a collection with a list of items called "categories" each category has a name field, its that name i want to push into the meteor.user.

Its supposed to work in a way that the user can push as many of these names as they want however its only accepting one entry, and when i click on another name, the new name replaces the old one, instead of being an array. how can i make it so that it can allow many entries?

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

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

and this is the user from db.user.find() as you can see with

"name" : "a-reece"

its clearly pushing the name but i cannot add more, i can only replace

{ "_id" : "4CHcZjSD4hCrqweGA", "createdAt" : ISODate("2016-07-13T21:38:59.505Z"), "services" : { "password" : { "bcrypt" : "$2a$10$lKZtrYSMD4EGPj6eamgFDuPZ41Jw52DgivBly3lUYWbGDtfZBg1X." }, "resume" : { "loginTokens" : [ { "when" : ISODate("2016-07-13T21:38:59.719Z"), "hashedToken" : "BsqTGedB2FkmSPO3+5I31rOM2+MPtF97Zc9tRQ4pf8Y=" } ] } }, "emails" : [ { "address" : "[email protected]", "verified" : false } ], "roles" : [ "discoveror", "yes" ], "isAdmin" : true, "name" : "a-reece" }

how can i add more names instead of replacing?

EDIT

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

ANSWER

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

Upvotes: 0

Views: 817

Answers (1)

Michel Floyd
Michel Floyd

Reputation: 20227

You're currently doing:

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

You have two choices: $push or $addToSet:

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

or

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

The former pushes onto an array, allowing duplicates, the latter avoids dupes.

You don't need:

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

Upvotes: 2

Related Questions