Firestore 文档数据返回未定义

时间:2021-05-30 17:16:26

标签: firebase google-cloud-firestore

我在 firestore DB 上手动创建了一个集合,并手动添加了一个文档。但是当我查询数据库中存在的那个文档的数据时,我得到了文档数据的未定义。我已经添加了相关代码和控制台日志。

Code

export const addLikes = async (postId, userId) => {
  console.log(`postId : ${postId}`);
  console.log(`userId : ${userId}`);
  await firestore()
    .collection('likesComments')
    .doc(postId)
    .get()
    .then(documentSnapshot => {
      console.log(
        `documentSnapshot : ${JSON.stringify(
          documentSnapshot,
          getCircularReplacer(),
        )}`,
      );
      console.log(
        `documentSnapshot.data() : ${JSON.stringify(
          documentSnapshot.data(),
          getCircularReplacer(),
        )}`,
      );
    })
    .catch(e => console.log(e));
};

console log print

 LOG  postId : eJh3e7LcU4tGLfnw3Rbr
 LOG  userId : pdPu80SlluMVQgGiz6QvY1HAm022
 LOG  documentSnapshot : {"_metadata":{"_metadata":[false,false]},"_ref":{"_firestore":{"_app":{"_name":"[DEFAULT]","_deleted":false,"_options":{"storageBucket":"react-native-social-media-app.appspot.com","databaseURL":null,"apiKey":"AIzaSyAgkR8wcB4yTFcTp5_G210uiu_v0Ammv-A","gaTrackingId":null,"projectId":"react-native-social-media-app","messagingSenderId":"137374136152","appId":"1:137374136152:android:6f935d6007f819bc4dd72d"},"_automaticDataCollectionEnabled":true,"_initialized":true,"_nativeInitialized":true},"_nativeModule":{"RNFBFirestoreModule":true,"RNFBFirestoreCollectionModule":true,"RNFBFirestoreDocumentModule":true,"RNFBFirestoreTransactionModule":true},"_config":{"statics":{"CACHE_SIZE_UNLIMITED":-1},"version":"11.4.1","namespace":"firestore","nativeModuleName":["RNFBFirestoreModule","RNFBFirestoreCollectionModule","RNFBFirestoreDocumentModule","RNFBFirestoreTransactionModule"],"nativeEvents":["firestore_collection_sync_event","firestore_document_sync_event","firestore_transaction_event"],"hasMultiAppSupport":true,"hasCustomUrlOrRegionSupport":false},"_referencePath":{"_parts":[]},"_transactionHandler":{"_pending":{}}},"_documentPath":{"_parts":["likesComments","eJh3e7LcU4tGLfnw3Rbr"]}},"_exists":false}
 LOG  documentSnapshot.data() : undefined

1 个答案:

答案 0 :(得分:0)

我认为您应该像这样在 then 块中调用 .data() :

.then(documentSnapshot => {
      console.log(
        `documentSnapshot : ${JSON.stringify(
          documentSnapshot.data(),
          getCircularReplacer(),
        )}`,
      );

相关问题