简体   繁体   English

为什么我在反应原生 Firebase 成功异步调用后收到错误

[英]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.

相关问题 为什么我在 React 中收到“无效的 Hook Call”错误? - Why am I receiving 'Invalid Hook Call' error in React? 成功推文后,我没有收到 Twitter 的回复 - I am not receiving response from Twitter after after successful tweet 为什么我会收到这个 Invalid Hooks Call 错误? - Why am I receiving this Invalid Hooks Call error? firebase.database 不是 function 为什么我在反应本机应用程序中收到此错误? - firebase.database is not a function why I am getting this error in react native app? 使用react-router成功进行异步调用后如何重定向? - How to redirect after successful Async call with react-router? Chrome 本地消息传递 — 为什么我会收到“未找到指定的本地消息传递主机”错误? - Chrome Native Messaging — Why am I receiving a “Specified native messaging host not found” error? 在 React 本机 useEffect 中,使用异步调用我得到一个未定义的,在以下情况下如何避免未定义? - In React native useEffect ,using async call i am getting a undefined , How to avoid that undefined in the below case? 在本机反应中收到 fcm 通知后显示来电 - display incoming call after receiving fcm notification in react native "我在 React Native 中遇到了关于异步存储的问题" - i am facing a problem regarding async storage in react native 为什么我在React中收到有关分配键的错误? - Why am I receiving errors about assigning keys in React?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM