4

I want to update email address in meteor by :

Meteor.users.update(this._id, {$set: {"emails[0].address": "deleted_" + preEmail }});

but instead of updating email array, 0 argument, then address ,it create a new field like emails[0] then address. it is interesting that in one other .js file it works correctly!

Amit kumar
  • 6,029
  • 6
  • 30
  • 40
Mehrnoosh
  • 879
  • 2
  • 12
  • 27

4 Answers4

6

The Accounts package comes with pre-built functions for changing the email address.

import { Accounts } from 'meteor/accounts-base'

//get old email
const oldEmail = Meteor.users.findOne(this.userId).emails[0].address;

//add new email
Accounts.addEmail(this.userId, args.email);

//remove old email
Accounts.removeEmail(this.userId, oldEmail);
Sean
  • 2,609
  • 1
  • 18
  • 34
6

You were almost there. In you code change "emails[0].address" with emails.0.address . My code is working in Angular 2 meteor. I hope it will work for you also :)

 Meteor.users.update({
        _id: id
     }, 
     {
        $set: {
            'emails.0.address': address,
            "username": username
        }
    });
Amit kumar
  • 6,029
  • 6
  • 30
  • 40
0

You could setup the array first and then update emails like this

var newEmail = [{address:email, verified: false}]
Meteor.users.update({_id:this._id}, {$set: {emails:newEmail }});
yusha uzumo
  • 211
  • 1
  • 4
  • 15
-2

I think the issue is that your filter is wrong.

Meteor.users.update({_id:this._id}, {$set: {"emails[0].address": "deleted_" + preEmail }});
mutdmour
  • 543
  • 5
  • 14