【问题标题】:Firebase get request returning empty array?Firebase获取请求返回空数组?
【发布时间】:2020-05-16 19:02:12
【问题描述】:

我相信这可能只是我的逻辑错误,但我不确定问题到底出在哪里,我正在寻求帮助调试。我正在使用 Firebase Cloud Firestore。我试图通过我的“关注”集合进行查询,以返回键“senderHandle”等于参数句柄的所有数据,然后将该数据推送到空数组 followData。然后,遍历每个 followData 并对“posts”集合进行文档查询调用。然后,循环遍历“posts”集合中返回的每个文档,并将每个数据推送到空数组“posts”中。

当我尝试返回帖子数组时,尽管它返回空。我的总体目标是获取 params 处理的所有用户,循环遍历每个用户以获取他们的帖子,然后将他们的帖子推送到一个空数组中。

功能代码:

// fetch home-specific posts (of users following)
exports.getHomePosts = (req, res) => {
  let posts = [];
  let followData = [];
  // get following users
  const followDocument = db
    .collection("follows")
    .where("senderHandle", "==", req.params.handle);

  followDocument
    .get()
    .then((data) => {
      if (data.query.size == 0) {
        return res.status(400).json({ error: "Not following any users" });
      } else {
        data.forEach((doc) => {
          followData.push(doc.data());
        });
      }
    })
    .then(() => {
      followData.forEach((follow) => {
        db.collection("posts")
          .where("userHandle", "==", follow.receiverHandle)
          .where("location", "==", "explore")
          .get()
          .then((data) => {
            data.forEach((doc) => {
              posts.push({
                postId: doc.id,
                body: doc.data().body,
                userHandle: doc.data().userHandle,
                createdAt: doc.data().createdAt,
                commentCount: doc.data().commentCount,
                likeCount: doc.data().likeCount,
                userImage: doc.data().userImage,
              });
            });
          });
      });
      return res.json(posts);
    })
    .catch((err) => {
      res.status(500).json({ error: err.message });
    });
};

followData 返回:

[
    {
        "receiverHandle": "John Doe",
        "senderHandle": "bear"
    },
    {
        "senderHandle": "bear",
        "receiverHandle": "Yikies"
    },
    {
        "receiverHandle": "bear",
        "senderHandle": "bear"
    },
    {
        "receiverHandle": "anon",
        "senderHandle": "bear"
    },
    {
        "senderHandle": "bear",
        "receiverHandle": "BigYikes"
    }
]

【问题讨论】:

    标签: javascript arrays json firebase debugging


    【解决方案1】:

    此代码存在多个问题。

    1. 您不是waiting promise 待解决。
    2. 多个returns 语句
    3. 您不会创建像 postsfollowData 这样的全局数组[您可以返回然后进行下一次回调]

    代码:

    // fetch home-specific posts (of users following)
    
    exports.getHomePosts = (req, res) => {
      const followDocument = db
        .collection("follows")
        .where("senderHandle", "==", req.params.handle);
    
      followDocument
        .get()
        .then((data) => {
          if (data.query.size == 0) {
            throw new Error("NOT_FOUND");
          } else {
            return data.map((doc) => doc.data());
          }
        })
        .then((followData) => {
          const promises = followData.map((follow) => {
            return db
              .collection("posts")
              .where("userHandle", "==", follow.receiverHandle)
              .where("location", "==", "explore")
              .get();
          });
          Promise.all(promises).then((results) => {
            const posts = results
              .map((data) => {
                return data.map((doc) => ({
                  postId: doc.id,
                  body: doc.data().body,
                  userHandle: doc.data().userHandle,
                  createdAt: doc.data().createdAt,
                  commentCount: doc.data().commentCount,
                  likeCount: doc.data().likeCount,
                  userImage: doc.data().userImage,
                }));
              })
              .flat();
            return res.json(posts);
          });
        })
        .catch((err) => {
          if (err.message === "NOT_FOUND") {
            return res.status(400).json({ error: "Not following any users" });
          }
          res.status(500).json({ error: err.message });
        });
    };
    

    【讨论】:

    • 您好,感谢您的快速回复!我收到 data.map 不是函数的错误,我认为这是因为 data 是对象而不是数组。我的替代解决方案是 Objects.keys 吗?
    • 是的!.. 我不确定你的结果。请尝试控制台日志并检查.. 我只是强调了你可以纠正的错误和一个粗略的示例。
    • 非常感谢!我会再弄乱代码,感谢您的指导!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多