[英]Why am i receiving an error after a successful async call in react native Firebase
I'm still learning about react-native.我还在学习 react-native。
After I make a successful call to delete a file from my record, I then try to attempt to print out a success message to the console but instead I'm receiving an error stated below.在我成功调用从我的记录中删除文件后,我尝试尝试将成功消息打印到控制台,但我收到了如下所述的错误。 Any pointers to where I'm going wrong?任何指向我哪里出错的指针?
TypeError: undefined is not an object (evaluating
'server.deleteLocation(userUID, {
streetName: streetName,
long: long,
lat: lat
}).then')
Here is my firebase call (in a seperate file. Ive checked this code and it 100% works:):这是我的 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;
});
};
Here is my calling code (here is where the problem occurs)这是我的调用代码(这里是问题发生的地方)
........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);
});
};
Your method deleteLocation
that you have written on separate file is not async
method, and you are trying to access .then
on that method so that's why this error is coming.您在单独文件上编写的方法deleteLocation
不是async
方法,并且您正在尝试在该方法上访问.then
,这就是出现此错误的原因。 Here is the solution.这是解决方案。
Make your deleteLocation
async:使您的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;
});
};
Now, you can access .then
and error will gone.现在,您可以访问.then
并且错误将消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.