簡體   English   中英

使用 Firebase Cloud Functions 更新用戶

[英]Update user with Firebase Cloud Functions

我想通過電話號碼查找用戶並使用 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”

雖然我不明白這個例外,但我想不出不同的方法來做到這一點。 任何的想法?

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']));

如果您的雲函數是后台觸發的,請不要忘記返回 Promise 鏈,請參閱https://firebase.google.com/docs/functions/terminate-functions

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM