使用 Firebase Cloud Functions 更新用户

Update user with Firebase Cloud Functions

我想通过 phone 号码查找用户,并使用 Admin SDK 在 Firebase Cloud Functions 中更新他们的 displayName。 例如,当我尝试 运行 此代码时:

import * as admin from 'firebase-admin'
admin.auth().getUserByPhoneNumber(phone).then((user) => {
   user.displayName = newName;
}).catch((reason) => console.log(reason['message']));

我收到以下消息:

Cannot assign to read only property 'displayName' of object '#UserRecord'

虽然我不理解这个例外,但我想不出有什么不同的方法来做到这一点。有什么想法吗?

getUserByPhoneNumber() method returns a UserRecord 没有任何修改 user 对象的方法。

需要使用updateUser()方法来实现,如下:

//....
return admin.auth().getUserByPhoneNumber(phone)
.then(userRecord => {
   return admin.auth().updateUser(userRecord.uid, {displayName: newName});
})
.catch((reason) => console.log(reason['message']));

如果您的 Cloud Function 是后台触发函数,请不要忘记 return Promise 链,请参阅 https://firebase.google.com/docs/functions/terminate-functions