首页 > 解决方案 > 当来自 rnfirebase 的 firestore 包时,数据未存储在 Firestore 中

问题描述

我为我的 React 本机应用程序使用了 @react-native-firebase/firestore 包。我也安装了我的 firebase/app,并按照 firebase 网页中的所有安装步骤进行操作。但我无法将数据存储到我的 Firestore 中。这是我使用的代码。

import firestore from '@react-native-firebase/firestore';

export const storeData = async (userDetails) => {
  let userName = userDetails.userName;
  let storeSuccess = await firestore()
    .collection('Users')
    .doc('123')
    .set({
      username: userDetails.userName,
      userselfie: userDetails.userSelfie,
      LP: userDetails.LPImage,
      bill: userDetails.BillImage,
      customercontact: userDetails.customerContact,
      date: userDetails.date,
      time: userDetails.time,
    })
    .then(() => console.log('success'))
    .catch((error) => {
      error.message && alert(error.message);
      console.log(
        'Something went wrong while adding user data to firestore: ',
        error,
      );
      return 'error';
    });
  return storeSuccess;
};

我已经完成了所有工作,甚至调用了上述函数,但我得到了{"_U": 0, "_V": 0, "_W": null, "_X": null}来自 firestore 函数的响应。

标签: firebasereact-nativegoogle-cloud-firestorereact-native-firebase

解决方案


使用async/awaitwithPromise.then()可能会导致这种意外行为。为了修复您的功能,请删除async/await并保留then()如下符号:

export const storeData = (userDetails) => {
let userName = userDetails.userName; 
let storeSuccess = firestore()
.collection('Users')
    .doc('123')
    .set({
      username: userDetails.userName,
      userselfie: userDetails.userSelfie,
      LP: userDetails.LPImage,
      bill: userDetails.BillImage,
      customercontact: userDetails.customerContact,
      date: userDetails.date,
      time: userDetails.time,
    })
    .then(() => console.log('success'))
    .catch((error) => {
      error.message && alert(error.message);
      console.log(
        'Something went wrong while adding user data to firestore: ',
        error,
      );
      return 'error';
    });
  return storeSuccess;
};

推荐阅读