Strange behavior: Unable to make changes to user via MongoDB

This is really weird.

I’m having trouble updating a user’s account object via Meteor.users.update() on the server side. For example, running this update on server:

Meteor.users.update({_id: this.userId}, {$set: {subscription: subscription}})

Produces no updates or addition of the subscription object, when it should. I have the same logic on another Meteor app and it works fine. When I add a callback to see whether or not the update occurred, it comes back with a success, so clearly it’s supposed to work.

If you do the following:

console.log(Meteor.users.update({_id: this.userId}, {$set: {subscription: subscription}}));

what do you get back? The expected value would be 1, meaning one record was updated. The follow-up would be trying to $set something else, in my opinion?

Yeah, I get a 1. Doing that or console logging the result when you add a callback all gives 1. But nothing is updated. Really weird.

Next thoughts would be a hook/schema on the collection that is stripping the subscription field?

Not sure what you mean.

I don’t use any schema for my database. Care to elaborate?

Just trying to figure out how you would be getting a successful update without actually changing the value in question. If you do something like { $set: { description: 'test_description' } }, and you get back a 1 with no data seemingly changing, that to me says that there is something between your update call and your database. Could be a schema with an autovalue, or a pre (or post) update hook that is toying with the value you are trying to set.

Have you tried setting just a hard-coded value for subscription? I’d also consider adding a modifiedAt: new Date() timestamp to your update, so that you can confirm you are touching the record you think you are touching.

If this is app-wide, I wouldn’t expect the following to be the culprit, but if you are experiencing this with a particular userId, could you possibly have duplicated that _id? If you don’t include a { multi: true } with your update, and your query matches multiple records, you can still get a 1 back, but not necessarily have updated the record you are looking at.

1 Like

Thanks for your thoughtful reply.

I was playing around when I found out that this.userId is undefined on the server. That’s probably why I’m getting this error.

Could it be that it’s because I’m using ES6 syntax in the methods? I’ll try to find out.

Edit: It seems like the error was caused because I used the ES6 arrow functions. I forgot about the arrow functions and their lexical scoping.

1 Like