javascript - Firebase 更新配置文件未在第一次尝试中更新配置文件 || 承诺不处理 || 显示名称
问题描述
我正在使用带有 React Native 的 firebase phone auth 对用户进行身份验证,我想将 displayName 添加到当前用户,但在创建帐户期间无法添加,因此在阅读文档后,我正在添加这样的显示名称。
export const otpCustomer = user => async dispatch => {
dispatch({type: OTP_USER_LOADING});
try {
const verification = 'unverified';
const date = Date.now()
const AccountStatus = 'open'
const Devicetoken = {os: 'default', token: 'not defined'};
const {phone, name , userId } = user;
const type = 'customer'
let usp = await auth().currentUser.updateProfile({
displayName:'customer'
});
console.log('usppppp',usp)
const req = await fetch(
`https://url.firebaseio.com/customers/${userId}.json`,
{
method: 'put',
headers: {
ContentType: 'application/json',
},
body: JSON.stringify({name,phone,verification,
Devicetoken,date,AccountStatus,customerId:userId,type}),
},
);
const res = await req.json()
if (res.error) {
dispatch({
type: OTP_USER_FAIL,
payload: 'error',
});
} else {
dispatch({
type: OTP_USER_SUCCESS,
payload: res,
});
}
} catch (err) {
console.log('err',err)
dispatch({
type: OTP_USER_FAIL,
payload: 'err',
});
}
};
当我运行此功能配置文件未更新时,显示名称仍然为空,但是当第二次用户登录时,它正在添加显示名称。我知道这个问题与第一次没有处理的承诺有关,所以我该如何处理。
解决方案
我不确定您的代码中有什么不起作用,但是这个最小的示例对我有用:
firebase.auth().signInAnonymously().then(function() {
firebase.auth().currentUser.updateProfile({
displayName:'customer '+new Date()
}).then(function() {
console.log(firebase.auth().currentUser.displayName);
});
});
每次我运行代码时,显示名称都会立即更新为当前日期/时间。
推荐阅读
- dropzone.js - Dropzonoe 限制一次上传文件的数量,并显示错误信息
- c - 在 C 中除法时如何防止整数截断值?
- javascript - 链接上传的文件供 python 使用
- python - 如何在 python 中获取文章的维基百科外链接?
- visual-studio-code - 仅适用于打开工作区的快速文件导航
- c - 通用插入排序无法对浮点数组进行排序
- php - DROP TRIGGER IF EXISTS 返回触发器不存在
- typescript - typescript 类实现签名不正确的接口方法时没有错误
- reactjs - 如何在反应表附件中使用三元运算符
- c - 当内核模块在不禁用中断的情况下持有自旋锁时从用户空间发出 IOCTL 时是否会发生上下文切换?