【发布时间】:2021-01-11 15:27:57
【问题描述】:
我将在下面解释这个问题,但可以说我认为这条线在 React 中对我有用,但在 React Native 中不适用:
const postDocument = firebase.firestore().doc(`/posts/${id}`);
然后我尝试写这个,它返回了一些奇怪的 Promise:
const postDocument2 = firebase.firestore().collection('posts').where('id', '==', id);
我想做的很清楚是在“hugPost”函数中使用这个变量 postDocument ,下面将提供代码。它适用于 React,但不适用于 React Native。也许变量 postDocument 不是问题我不确定。
hugPost = (id) => {
const hugDocument = firebase.firestore()
.collection('likes')
.where('userHandle', '==', this.uid)
.where('postId', '==', id)
.limit(1);
const postDocument = firebase.firestore().doc(`/posts/${id}`);
let postData;
//postDocument2
postDocument
.get()
.then((doc) => {
if (doc.exists) {
postData = doc.data();
console.log("checking");
postData.postId = doc.id;
return hugDocument.get();
} else {
return (
alert({
error: 'Post not found'
}));
}
})
.then((data) => {
if (data.empty) {
return firebase.firestore()
.collection('likes')
.add({
postId: id,
userHandle: this.uid
})
.then(() => {
postData.likeCount++;
return postDocument.update({ likeCount: postData.likeCount });
})
.then(() => {
return (postData);
});
} else {
return (
alert({
error: 'Post already liked'
}));
}
})
.catch((err) => {
alert(
"Something went wrong, check your internet connection please",
err
);
});
};
在 React Native 中,该函数接收到正确的 id 但不起作用,它直接进入 .catch,说我的警报消息“出现问题,请检查您的互联网连接”并引发 [object object] 错误。有什么帮助吗?
它将这 3 个中的 NONE 识别为我想要的文档,该文档位于集合帖子中并具有字段 id(但它在 React 中识别):
const postDocument2 = firebase.firestore().collection('posts').where('id', '==', id);
const postDocument = firebase.firestore().doc(`/posts/${id}`);
const postDocument3 = firebase.firestore().collection('posts').doc('qRHqV8w6iyx4yHWg76tN');
【问题讨论】:
-
[object object]不是很有用。如果你JSON.stringify(err)怎么样? -
我写了它,它又返回了 [object object]..
-
没有可能的 JSON.stringify 输入会返回“[object object]”。也许只是 console.log(err) 并告诉我们那是什么?
-
undefined 不是一个对象(评估 'data.empty')
-
没关系我即将解决它。
标签: javascript firebase react-native google-cloud-firestore