当我将变量作为文档引用传递时无法获取文档快照
Not able to get document snapshot when I pass a variable as document reference
首先,我是第一次问关于堆栈溢出的问题,对于在提问过程中出现的任何错误,我深表歉意。
我正在尝试获取节点 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 是我通过变量传递的文档引用 ID“ request.body.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);
});
此代码能够成功地将数据记录到控制台,而上面的代码则不能,尽管两种情况下的文档引用相同。请帮忙
好的。我找到了解决方案,这对我来说是非常愚蠢的错误。似乎我作为文档引用传递的 id 在末尾有一些空格,当我使用 string.trim() 时它解决了这个问题。干杯
首先,我是第一次问关于堆栈溢出的问题,对于在提问过程中出现的任何错误,我深表歉意。
我正在尝试获取节点 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 是我通过变量传递的文档引用 ID“ request.body.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);
});
此代码能够成功地将数据记录到控制台,而上面的代码则不能,尽管两种情况下的文档引用相同。请帮忙
好的。我找到了解决方案,这对我来说是非常愚蠢的错误。似乎我作为文档引用传递的 id 在末尾有一些空格,当我使用 string.trim() 时它解决了这个问题。干杯