繁体   English   中英

当我将变量作为文档引用传递时无法获取文档快照

[英]Not able to get document snapshot when I pass a variable as document reference

首先,我是第一次问有关堆栈溢出的问题,对于提问时的任何错误,我深表歉意。

我正在尝试获取我在 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);
  });

此代码能够成功地将数据记录到控制台,而上面的代码则不能,尽管两种情况下的文档引用相同。 请帮忙。

行。 我找到了解决方案,这对我来说是非常愚蠢的错误。 似乎我作为文档参考传递的 id 在末尾有一些空格,当我使用 string.trim() 时它解决了这个问题。 干杯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM