0

I need to add an additional field inside Users collection of meteor. So what I am doing is in the below server.js file I am adding following code.

//server.js

Meteor.publish("users", function () {
  if (this.userId) {
    return Meteor.users.find({_id: this.userId},
        {fields: {'notified': 1}});
  } else {
    this.ready();
  }
});

The client.js is as follows:

// client.js
Deps.autorun(function() {
    Meteor.subscribe('users');
});

The fixture.js is as follows in order to add the field while a new user logged-in:

// fixture.js
Accounts.onCreateUser(function (options, user) {
    if (options.profile) {
        user.profile = options.profile;
        user.profile.notified = false;
    }
    return user;
});

In the code I am updating the notified field like this

// inside a loop taking all the users of the collection
// where myUsers is the loop index.
Meteor.users.update(myUsers._id, {$addToSet: {notified : 1}});

But to my surprise when I am checking whether the field has been added in the collection either via mongo console or browser console, it is not showing up. Why so?. I have read almost all the articles available on net and followed http://docs.meteor.com/#meteor_users, still not working. So, anyone knows what should be done? I am clueless.

user3374348
  • 4,101
  • 15
  • 29
priya
  • 81
  • 2
  • 10
  • Is the `Meteor.users.update(myUsers._id, {$addToSet: {notified : 1}});` update happening on the server or on the client? – user3374348 May 12 '14 at 13:41

2 Answers2

0

You didn't write to user.notified, but to user.profile.notified. These are two different fields!

Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • I have tried that also, still not working...Is there anything that I am missing. Please let me know – priya May 12 '14 at 13:25
0

The publish is fine.

I created a project with your snippets and I see the value of notified for a user client side. I suspect that you aren't actually updating the users. You want to make sure that you are doing it server side.

As Hubert said you want to make sure where you want the notified field to be. Either in the base of the user or in the profile section, something to keep in mind is that if it's in the profile section a user can edit their own profile section:

Meteor.users.update( Meteor.user()._id, { $set: { 'profile.notified': 1 } } );

Check all the fields of a logged in user server side to make sure you have a notified value by adding this server side:

Meteor.methods({
  user: function() {
    console.log(Meteor.users.findOne(this.userId));
  }
});

and then typing in Meteor.call('user'); in your js console. Look for the result in your terminal window.

Also your line

Meteor.users.update(myUsers._id, {$addToSet: {notified : 1}});

Would result in notified being:

notified: [ 1 ]

i.e. addToSet is creating an array where one doesn't exist and then adding the value 1 to it.

I would also check on your loop code to. myUsers is the loop index? Shouldn't it be the user object from a loop.

I would expect something more like:

Meteor.users.find().forEach(function (user) {
  Meteor.users.update( user._id, { $set: { notified: 1 } } );
});

if you wanted to make all users notified (you would probably want to select by {notified: 0} for efficiency though). This also simply makes notified: 1 rather than [1]

shambles
  • 666
  • 4
  • 5
  • Hi, I did all the changes that you told.... so the output that am getting on the console is " { createdAt: Tue May 13 2014 10:56:45 GMT+0100 (BST), _id: 'dHKSZnqNApyPia9xn', services: { password: { srp: [Object] }, resume: { loginTokens: [Object] } }, username: 'ajay', emails: [ { address: 'ajay@gmail.com', verified: false } ] } " ..... Also, I want to check for all the users that already have account in the app but when I am running the loop as your's above, it is giving only current logged-in user's information. Why so and what should be done to get all user's info. – priya May 13 '14 at 09:58
  • You need to do the Meteor.users.find().forEach() server side to get all users. By default a user on the client only gets to see themself. By running the loop I gave above you would get the notified field in the base of the user object i.e. { createdAt: "", _id: "", notified: 1, emails: [{...}]} as opposed to in the profile section. I believe by default the accounts-password system doesn't use the profile section as opposed to the facebook, github etc. oauth flows. if you wanted to make it in the profile section do $set: { 'profile.notified': 1 }. – shambles May 13 '14 at 10:13
  • Thanks @boingy, it worked. Can you help me in this issue also. "http://stackoverflow.com/questions/23108369/nested-array-update-in-mongodb/23152195?noredirect=1#comment36143427_23152195" – priya May 13 '14 at 13:04