小编Muh*_*las的帖子

Promise 在上一个 then 执行完成之前执行 then 函数

我尝试链接几个按顺序执行的 then 函数,但最后一个 .then() 是在前一个 .then() 执行完成之前执行的,因此它发送了一个空的有效负载。以下是片段:

router.get("/selectedHotels", function(req, res) {
  let payload = [];
  return collectionRef
    .where("isOwner", "==", true)
    .get() //fetches owners
    .then(snapshot => {
      snapshot.forEach(user => {
        console.log("User", user);
        collectionRef
          .doc(user.id)
          .collection("venues")
          .get() // fetches hotels from owners
          .then(snapshot => {
            snapshot.forEach(doc => {
              if (
                doc.data().location.long == req.query.long &&
                doc.data().location.lat == req.query.lat
              ) {
                console.log(doc.id, "=>", doc.data());
                payload.push({
                  id: doc.id,
                  data: doc.data()
                });
              }
            });
          })
          .catch(err => {
            console.log("No hotels of this user", err);
          });
      }); …
Run Code Online (Sandbox Code Playgroud)

javascript node.js promise firebase es6-promise

2
推荐指数
1
解决办法
1497
查看次数

标签 统计

es6-promise ×1

firebase ×1

javascript ×1

node.js ×1

promise ×1