Trung Tran
Trung Tran

Reputation: 13771

Updating attribute not working properly Meteor / MongoDB

I am trying to update a document by clicking a button. However, I keep getting an "Internal error" message. The document I am trying to update is called "confirmed" and it can take true/false values.

Here's my methods.js:

Meteor.methods({

'confirmUser1': function(currUserId) {
    var currentUserId = currUserId;
    Meteor.users.update(currentUserId, {$set:
        {
            'confirmed': true
        }
    });
    console.log('user verified!');
}
});

Here's my template events helper:

Template.Users.events({
'click .confirmUser': function(e, tmpl) {
    e.preventDefault();
    var currentUserId = this._id;

    Meteor.call('confirmUser1', currentUserId, function(error) {
        if (error) {
            alert(error.reason);
        } else {
            console.log('success!');
            Router.go('Admin');
        }
    });
}
});

My button:

<p><button class="confirmUser">Confirm User</button></p>

Note: I used code very similar to this for a different update button/method and it worked fine... so I'm not sure what's going on here.

Upvotes: 0

Views: 108

Answers (1)

Ethaan
Ethaan

Reputation: 11376

try using Meteor.userId();, the context of this inside an event handler is quite different as using inside a Template.helper

If you do a console.log(currentUserId) you should get undefined.

So change this

var currentUserId = this._id;

to this.

var currentUserId = Meteor.userId();

Upvotes: 2

Related Questions