火力地堡user.updateProfile({...})不工作作出反应应用
问题描述:
所以,我有这个ReactJS应用程序,有一个用户数据库,火力地堡user.updateProfile({...})不工作作出反应应用
用于创建用户的功能是本
import { ref, firebaseAuth } from './../Components/config'
export function auth (email, pw) {
return firebaseAuth().createUserWithEmailAndPassword(email, pw)
.then(saveUser)
}
export function saveUser (user) {
return ref.child(`users/${user.uid}/info`)
.set({
email: user.email,
uid: user.uid,
number: "" //custom
})
.then(() => user)
}
正如你看到的用户是由3个属性,电子邮件,UID,和最初是“”自定义数字属性,
我有一个
changeNumberToNew = (n) =>{
var user = firebase.auth().currentUser;
if (user != null) {
user.updateProfile({
number: n
}).then(() => {
console.log("Number changer");
}).catch((error) => {
console.log(error);
});
} else {
console.log("No user")
}
};
的
和一个按钮来调用函数
<button onClick={this.changeNumberToNew(4)}>Click to change number</button>
当我点击按钮的承诺是分解导致的
console.log("Number changer")
执行,但是当我去看看火力数据库对象..没有什么变化,即使重装和等待仍然没有任何变化
答
我认为这里的问题是你的数据库中的用户对象与认证模块中的用户混淆。他们不一样。
当您在第一个块中表示以下内容时,可以将用户的“副本”保存到数据库中。
ref.child(`users/${user.uid}/info`)
.set({
email: user.email,
uid: user.uid,
number: ""
})
然后在第二个代码块中,尝试更新认证模块中的当前用户。不好。你应该更新你的数据库,而不是你的认证模块。
var user = firebase.**auth()**.currentUser
if (user != null) {
user.updateProfile({...})
}
我不认为你可以在认证模块中的当前用户创建一个自定义字段。 updateProfile()用于更新默认情况下从提供者处获得的字段,例如电子邮件,显示名称,photoURL等。您无法创建新字段。
您应该在您的数据库中更新用户的副本,然后在需要“号码”值时引用该副本。
您更改功能也许应该更喜欢......
changeNumberToNew = (n) => {
var user = firebase.auth().currentUser;
if (user) {
ref.child(`users/${user.uid}/info`).update({number: n})
.then(() => console.log("Number changer"))
.catch(error => console.log(error))
} else {
console.log("No user")
}
}
答
火力地堡验证updateProfile
只支持displayName
和photoURL
。它不支持客户端自定义属性。对于管理员自定义属性,您需要使用管理软件开发工具包:https://firebase.google.com/docs/auth/admin/custom-claims#set_and_validate_custom_user_claims_via_the_admin_sdk
在这种情况下,您最好在数据库中保存这些任意的自定义字段(假设它们不需要管理员权限)。
[Firebase v3 updateProfile方法]的可能重复(https://stackoverflow.com/questions/38559457/firebase-v3-updateprofile-method) –