【发布时间】:2020-11-12 00:25:44
【问题描述】:
我正在尝试使用 firebase 在 updateProfile 的 then 中使用 redux 进行 api 调用,但出现此错误:
TypeError: Cannot read property 'fetchProfile' of undefined
componentDidUpdate(prevProps) {
let user = firebase.auth().currentUser;
user.updateProfile({
displayName: `${form.firstName} ${form.lastName}`,
email: form.email,
phoneNumber: `${form.countryCode}${form.phone}`,
}).then(function() {
this.props.fetchProfile();
Navigation.pop(componentId);
}).catch(function(error) {
console.log("error edit profile: ",error,error.response);
this._registerFMSG.showMessage({
message: '',
description: error,
});
});
}
【问题讨论】:
-
this可能不是指您认为该承诺中的对象。 -
使用箭头函数,而不是常规函数。常规函数有自己的
this定义,箭头函数使用它们的上下文。 -
在
componentDidUpdate里面我怎么用箭头函数呢? -
@RowanX
.then(() => { this.props.fetchProfile()....
标签: reactjs firebase react-native firebase-authentication