سؤال

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.

هل كانت مفيدة؟

المحلول

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]

نصائح أخرى

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top