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

问题描述

首先,我是第一次在堆栈溢出时提出问题,并想在问问题时为任何错误道歉。

我正在尝试获取节点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()时,它解决了此问题。干杯