为什么在 React Native Firebase 中异步调用成功后我会收到错误
2020-12-08
34
我仍在学习 react-native。
成功调用从记录中删除文件后,我尝试将成功消息打印到控制台,但收到以下错误。有什么提示我哪里出错了吗?
TypeError: undefined is not an object (evaluating
'server.deleteLocation(userUID, {
streetName: streetName,
long: long,
lat: lat
}).then')
这是我的 firebase 调用(在单独的文件中。我检查过此代码,它 100% 有效!):
const deleteLocation = (uid, locationObject) => {
console.log('firebase deleteLocation............ START');
firestore()
.collection('Users')
.doc(uid)
.update({
locations: firestore.FieldValue.arrayRemove({
streetName: locationObject.streetName,
longitude: locationObject.long,
latitude: locationObject.lat,
}),
})
.then(() => {
console.log('firebase deleteLocation............ SUCCESS');
})
.catch((errorsss) => {
console.log('firebase deleteLocation............ FAILED');
throw errorsss;
});
};
这是我的调用代码(问题发生在这里)
........imports
import * as server from '../api/firebase';
deleteLocation = (streetName, long, lat) => {
server
.deleteLocation(userUID, { streetName, long, lat })
.then(() => {
console.log('DELETE WAS SUCESSSS');
})
.catch((error) => {
console.log('ERROR CAUGHT IN DELETING PROGRAM');
console.log(error);
});
};
1个回答
您在单独文件中编写的方法
deleteLocation
不是
async
方法,并且您尝试在该方法上访问
.then
,因此会出现此错误。以下是解决方案。
使您的
deleteLocation
异步:
const deleteLocation = async (uid, locationObject) => {
console.log('firebase deleteLocation............ START');
await firestore()
.collection('Users')
.doc(uid)
.update({
locations: firestore.FieldValue.arrayRemove({
streetName: locationObject.streetName,
longitude: locationObject.long,
latitude: locationObject.lat,
}),
})
.catch((errorsss) => {
console.log('firebase deleteLocation............ FAILED');
throw errorsss;
});
};
现在,您可以访问
.then
,错误将消失。
Kishan Bharda
2020-12-08