firebase - 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
解决方案
我认为您应该像这样在 then 块中调用 .data() :
.then(documentSnapshot => {
console.log(
`documentSnapshot : ${JSON.stringify(
documentSnapshot.data(),
getCircularReplacer(),
)}`,
);
推荐阅读
- c++ - OpenGL找不到一些统一变量
- python-3.x - 我有两个 numpy 数组列表,我希望它们进入一个 numpy 数组列表
- azure - 使用 terraform 进行增量更新
- erlang - 每个主管有多少工人?
- python - SyntaxError:名称“y”在全局变量之前使用
- arrays - 使用包含指令填充数组
- docker - 如何通过 SSH 将本地 Api docker-compose 项目部署到远程主机?
- python - 为什么 epoch 2 的时间是 epoch 1 的 18 倍?
- pdf - How to append PDF using itext 5
- java - Broadcast receiver needs Wifi in each receive.Is it safe to use wifi lock and wake lock on each receive?