【发布时间】:2020-08-19 06:59:27
【问题描述】:
首先,我是刚接触堆栈溢出问题的新手,对于提问时的任何错误,我深表歉意。
我正在尝试在 node js 中获取我在 firebase firestore 中的一些数据。代码如下:
firestoreDatabaseName
.collection("collectionName")
.doc(request.body.id)
.get()
.then((snapshot) => {
if (snapshot.exists) {
console.log(request.body.id);
console.log(snapshot.data());
response.send("Document found.");
} else {
console.log("Document doesn't exist", request.body.id);
response.send("Document doesn't exist.");
}
})
.catch((err) => {
console.log(err);
});
当我运行上面的代码时,登录到控制台的结果是:“文档不存在 5TLopY8RCHeUZolA0d2v”,5TLopY8RCHeUZolA0d2v 是我通过变量“request.body.id”传递的文档引用 ID .我已经签入了firestore,并且该文档存在。现在,当我通过将相同的文档引用 ID 作为字符串传递来运行上述代码时,我得到了快照和数据。这是有效的代码:
firestoreDatabaseName
.collection("collectionName")
.doc("5TLopY8RCHeUZolA0d2v")
.get()
.then((snapshot) => {
if (snapshot.exists) {
console.log(snapshot.data());
response.send("Document found.");
} else {
response.send("Document doesn't exist.");
}
})
.catch((err) => {
console.log(err);
});
此代码能够成功地将数据记录到控制台,而上面的代码则不能,尽管文档引用在两种情况下都是相同的。请帮忙。
【问题讨论】:
-
你能在你的第一个代码之前 console.log
request.body.id吗?我也想collectionName只是为了说明,对吧?在这两种情况下,您都将其更改为正确的吗? -
@Oliver 是的,奥利弗。 “collectionNameis”仅供参考。在这两种情况下,我都有正确的。我也登录了上面的 request.body.id,它仍然具有我传递的值,即“5TLopY8RCHeUZolA0d2v”。我以前做过很多次,但从来没有遇到过这个问题。
标签: javascript node.js firebase google-cloud-firestore firebase-admin