简体   繁体   中英

Update user collection

I've got a method which updates a user field. This works but just on client, if I reload the app this field starts from 0 again.

Method:

Meteor.methods({
    addPublication: function (publications) {
        check(publications, Number);

        Meteor.users.update(this.userId, {
            $set:{
                publications: publications
            }
        });
    }
});

Controller:

  Meteor.call('addPublication', Meteor.user().publications + 1);

Allow new values:

Meteor.users.allow({
    update: function(userId, user) {
        console.log('UPDATE USER');
        return true; 
    }
});

Publish:

  • Server

Meteor.publish('users', function() { return Meteor.users.find({}, { fields: { emails: 1, profile: 1, username: 1, publications: 1, } }); });

  • Client

Meteor.subscribe('users');

Am I missing something here?

You should publish your method also, otherwise this method does not make any change in the database

Meteor.publish('addPublication');

If you do allow update

Meteor.users.allow({
    update: function(userId, user) {
        console.log('UPDATE USER');
        return true; 
    }
});

There is no need from this method. You can just go on client:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM