【问题标题】:Update user with Firebase Cloud Functions使用 Firebase Cloud Functions 更新用户
【发布时间】:2020-05-31 17:35:22
【问题描述】:

我想通过电话号码查找用户,并使用 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']));

我收到以下消息:

无法分配给对象“#UserRecord”的只读属性“displayName”

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

【问题讨论】:

    标签: javascript firebase firebase-authentication google-cloud-functions firebase-admin


    【解决方案1】:

    getUserByPhoneNumber() 方法返回一个 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 是后台触发的,请不要忘记返回 Promise 链,请参阅 https://firebase.google.com/docs/functions/terminate-functions

    【讨论】:

      猜你喜欢
      • 2018-01-19
      • 2018-03-29
      • 1970-01-01
      • 2018-05-19
      • 2020-02-21
      • 2020-09-30
      • 2021-01-09
      • 2021-10-25
      • 2020-03-15
      相关资源
      最近更新 更多