简体   繁体   English

如何更新流星中的电子邮件地址?

[英]How can I update an email address in meteor?

Hi I am building an app using Meteor and need to update my email address. 嗨,我正在使用Meteor构建应用程序,需要更新我的电子邮件地址。 I am using the Meteor accounts package. 我正在使用流星帐户包。

My form passes an email value into an accountDetails object, which I will pass into a method to update my profile (including my email): 我的表单将电子邮件值传递到一个accountDetails对象中,该对象将传递给一种方法来更新我的个人资料(包括我的电子邮件):

Meteor.users.update({_id: this.userId},
{
   $set: {
            'emails.$.address': accountsDetail.email
    }

});

This gives me the error: 这给了我错误:

Exception while invoking method 'saveAccountInfo' MongoError: The positional operator did not find the match needed from the query. Unexpanded update: emails.$.address

Here is my user schema: 这是我的用户架构:

{
"_id" : "12345",

"emails" : [
    {
        "address" : "abc123@gmail.com",
        "verified" : false
    }

Can someone help? 有人可以帮忙吗? Thank you in advance! 先感谢您!

If you're sure the user has one address, which should be the case you can use emails.0.address instead of emails.$.address . 如果确定用户有一个地址,可以使用emails.0.address而不是emails.$.address

This should work for nearly all use cases. 这几乎适用于所有用例。 The exception is when there are many emails associated with a user. 例外是当有许多与用户关联的电子邮件时。 In this case: 在这种情况下:

If you are on the server & only on the server, you can use the positional operator to update a specific email, if there are multiple addresses. 如果您在服务器上并且仅在服务器上,则如果有多个地址,则可以使用位置运算符来更新特定的电子邮件。 You need to, in this case specify the current email in the query portion of the update. 在这种情况下,您需要在更新的查询部分中指定当前电子邮件。 Ie: {_id: this.userId, 'emails.$.address' : <current address> } 即: {_id: this.userId, 'emails.$.address' : <current address> }

The $ positional update operator is not currently available on the mongo client in Meteor. $位置更新运算符当前在Meteor的mongo客户端上不可用。

as each user is able to have multiple addresses (it´s an array - see http://docs.meteor.com/#/full/meteor_users for details) you need to specify which key you want to update (in this case the key is the address itself) 由于每个用户都可以拥有多个地址(它是一个数组-有关详细信息,请参见http://docs.meteor.com/#/full/meteor_users ),因此您需要指定要更新的密钥(在这种情况下,关键是地址本身)

Meteor.users.update({_id: this.userId, "emails.address":"me@domain.com"},
$set:{'emails.$.address': accountsDetail.email}
});

If every user only has one email you could also think about dropping this one and inserting the new one. 如果每个用户只有一封电子邮件,您还可以考虑删除该电子邮件并插入新的电子邮件。 see http://docs.mongodb.org/manual/reference/operator/update/pop/ for details. 有关详细信息,请参见http://docs.mongodb.org/manual/reference/operator/update/pop/

Hope this helps. 希望这可以帮助。

Regards, 问候,

René 雷内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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