Node.js Firestore更新文档字段和获取IMMIDATE响应问题获取旧数据

Node.js Firestore更新文档字段和获取IMMIDATE响应问题获取旧数据,node.js,google-cloud-firestore,Node.js,Google Cloud Firestore,我正在尝试更新firestore字段值。它正在更新,但我想立即使用更新的文档。当我当时获取这些数据时,它会在我第一次访问api时提供来自Firestore的旧数据。如果我两次点击相同的api,那么我会得到更新的数据 所以,我不明白真正的问题是什么 updateProductDetail: async(req, res)=>{ console.log("reqeuest", req.body.creator); try { var productFilterArray = []; v

我正在尝试更新firestore字段值。它正在更新,但我想立即使用更新的文档。当我当时获取这些数据时,它会在我第一次访问api时提供来自Firestore的旧数据。如果我两次点击相同的api,那么我会得到更新的数据

所以,我不明白真正的问题是什么

updateProductDetail: async(req, res)=>{
console.log("reqeuest", req.body.creator);
try {
  var productFilterArray = [];
  var counter = 0;        
  let collectionRef = db.collection("product_details_temp");
  let query = collectionRef.where('creator', '==', req.body.creator).get()
    .then(snapshot => {
      if (snapshot.empty) {
        console.log('No matching documents.');
        return;
      } else {
        snapshot.forEach(doc => {
          db.collection("product_details_temp").doc(doc.id).update({ "product": req.body.product });
        });

        collectionRef.where('creator', '==', req.body.creator).get()
          .then(snapshot => {
            let  a =[];
            snapshot.forEach(doc => {
              // a = doc.data();
              a.push(doc.data());
            });
            res.send(functions.responseGenerator(200, "successfull", a));
          })
      }
    })
  } catch (error) {
    res.send(
       functions.responseGenerator(error.code, error.message, error.data)
     );
   }

请帮助我

我也有同样的问题,我以这种方式尝试了代码:

updateProductDetail: async (req, res)=>{
 try {
  db.collection("product_details_temp")
   .where('creator', '==', req.body.creator).get()
    .then(snapshot => {
        snapshot.forEach(doc => {
          db.collection("product_details_temp").doc(doc.id).update({ 
        "product": req.body.product });
        });

        controller.getAPI(req,res);

      });
 } catch (error) {
   res.send(
    functions.responseGenerator(error.code, error.message, error.data)
  );
 }
},

 getAPI: async (req, res) =>{
  try{
   db.collection("product_details_temp").where('creator', '==', 
   req.body.creator).get()
  .then(snapshot => {
   var A = [];
     snapshot.forEach(doc => {
        A.push(doc.data());
    });
     res.send(functions.responseGenerator(200, "successfull", A));
   }) ;
  } catch (error) {
    res.send(
      functions.responseGenerator(error.code, error.message, error.data)
    );
    }
  }
};

我也有同样的问题,我用这种方式尝试代码:

updateProductDetail: async (req, res)=>{
 try {
  db.collection("product_details_temp")
   .where('creator', '==', req.body.creator).get()
    .then(snapshot => {
        snapshot.forEach(doc => {
          db.collection("product_details_temp").doc(doc.id).update({ 
        "product": req.body.product });
        });

        controller.getAPI(req,res);

      });
 } catch (error) {
   res.send(
    functions.responseGenerator(error.code, error.message, error.data)
  );
 }
},

 getAPI: async (req, res) =>{
  try{
   db.collection("product_details_temp").where('creator', '==', 
   req.body.creator).get()
  .then(snapshot => {
   var A = [];
     snapshot.forEach(doc => {
        A.push(doc.data());
    });
     res.send(functions.responseGenerator(200, "successfull", A));
   }) ;
  } catch (error) {
    res.send(
      functions.responseGenerator(error.code, error.message, error.data)
    );
    }
  }
};

我通过使用setTimeout()函数获得了此问题的解决方案:

setTimeout(() => {
          collectionRef.where('creator', '==', req.body.creator).get()
          .then(snapshot => {
           let a = [];
           snapshot.forEach(doc => {
           a.push(doc.data());
         });
        res.send(functions.responseGenerator(200, "successfull", a));
     })
    }, 2000);
将此代码与以下代码一起使用:

updateProductDetail: async(req, res)=>{
  try {
     let collectionRef = db.collection("product_details_temp");
     let query = collectionRef.where('creator', '==', req.body.creator).get()
         .then(snapshot => {
                if (snapshot.empty) {
                   console.log('No matching documents.');
                   return;
                } else {
                   snapshot.forEach(doc => {
                   db.collection("product_details_temp").doc(doc.id).update({ 
                   "product": req.body.product });
                  });
                }

      setTimeout(() => {
              collectionRef.where('creator', '==', req.body.creator).get()
              .then(snapshot => {
               let a = [];
               snapshot.forEach(doc => {
               a.push(doc.data());
             });
            res.send(functions.responseGenerator(200, "successfull", a));
         })
        }, 2000);

      })
   } catch (error) {
        res.send(
           functions.responseGenerator(error.code, error.message, error.data)
        );
   }

我通过使用setTimeout()函数获得了此问题的解决方案:

setTimeout(() => {
          collectionRef.where('creator', '==', req.body.creator).get()
          .then(snapshot => {
           let a = [];
           snapshot.forEach(doc => {
           a.push(doc.data());
         });
        res.send(functions.responseGenerator(200, "successfull", a));
     })
    }, 2000);
将此代码与以下代码一起使用:

updateProductDetail: async(req, res)=>{
  try {
     let collectionRef = db.collection("product_details_temp");
     let query = collectionRef.where('creator', '==', req.body.creator).get()
         .then(snapshot => {
                if (snapshot.empty) {
                   console.log('No matching documents.');
                   return;
                } else {
                   snapshot.forEach(doc => {
                   db.collection("product_details_temp").doc(doc.id).update({ 
                   "product": req.body.product });
                  });
                }

      setTimeout(() => {
              collectionRef.where('creator', '==', req.body.creator).get()
              .then(snapshot => {
               let a = [];
               snapshot.forEach(doc => {
               a.push(doc.data());
             });
            res.send(functions.responseGenerator(200, "successfull", a));
         })
        }, 2000);

      })
   } catch (error) {
        res.send(
           functions.responseGenerator(error.code, error.message, error.data)
        );
   }

听起来您有两个操作:

  • 写操作
  • 必须查看写入操作结果的查询/读取操作
  • 从Firestore读取数据或将数据写入Firestore的代码异步运行。为了防止阻塞应用程序,当读/写操作在后台运行时,允许继续正常的代码流。这意味着查询/读取当前在代码中运行,写入操作尚未完成,因此在查询中看不到该写入操作的结果

    解决方案是等待写入操作完成后再开始查询。您已经对查询执行了此操作,其中使用
    .then(snapshot=>{
    构造等待结果。您需要对
    更新(…)
    调用执行相同的操作,例如:

      let collectionRef = db.collection("product_details_temp");
      let query = collectionRef.where('creator', '==', req.body.creator).get()
        .then(snapshot => {
          if (snapshot.empty) {
            console.log('No matching documents.');
            return;
          } else {
            let promises = [];
            snapshot.forEach(doc => {
              promises.push(db.collection("product_details_temp").doc(doc.id).update({ "product": req.body.product }));
            });
            Promise.all(promises).then(() => {
              collectionRef.where('creator', '==', req.body.creator).get()
                .then(snapshot => {
                  let  a =[];
                  snapshot.forEach(doc => {
                    a.push(doc.data());
                  });
                  res.send(functions.responseGenerator(200, "successfull", a));
                })
            })
          }
    
    
    主要变化如下:

    • 此代码捕获
      promises
      数组中
      update
      调用的结果。这为我们提供了一个promises列表,在写入操作完成时解析
    • 然后,它使用
      Promise.all()
      等待所有写入操作完成,然后再开始查询

    听起来您有两种操作:

  • 写操作
  • 必须查看写入操作结果的查询/读取操作
  • 从Firestore读取数据或向Firestore写入数据的代码以异步方式运行。为了防止阻止应用程序,在后台运行读/写操作时,允许您继续正常的代码流。这意味着查询/读取当前在您的代码中运行,写操作尚未完成,因此您看不到结果在您的查询中写入操作的

    解决方案是等待写入操作完成后再启动查询。您已经对查询执行了此操作,其中使用了
    。然后(snapshot=>{
    构造来等待结果。您需要对
    更新(…)
    调用执行相同的操作,例如:

      let collectionRef = db.collection("product_details_temp");
      let query = collectionRef.where('creator', '==', req.body.creator).get()
        .then(snapshot => {
          if (snapshot.empty) {
            console.log('No matching documents.');
            return;
          } else {
            let promises = [];
            snapshot.forEach(doc => {
              promises.push(db.collection("product_details_temp").doc(doc.id).update({ "product": req.body.product }));
            });
            Promise.all(promises).then(() => {
              collectionRef.where('creator', '==', req.body.creator).get()
                .then(snapshot => {
                  let  a =[];
                  snapshot.forEach(doc => {
                    a.push(doc.data());
                  });
                  res.send(functions.responseGenerator(200, "successfull", a));
                })
            })
          }
    
    
    主要变化如下:

    • 此代码捕获
      promises
      数组中
      update
      调用的结果。这为我们提供了一个promises列表,在写入操作完成时解析
    • 然后,它使用
      Promise.all()
      等待所有写入操作完成,然后再开始查询

    非常感谢您仍然面临相同的问题谢谢您仍然面临相同的问题请不要使用超时来解决异步代码中的竞争条件。充其量,您让代码等待的时间比需要的时间长,最坏情况下,这对连接速度较慢的用户不起作用。请不要使用超时来解决异步代码中的竞争条件美国代码。充其量你让代码等待的时间比需要的时间长,最坏的情况下这对连接速度较慢的用户不起作用。这真的很酷:)非常感谢@frank它正在工作这真的很酷:)非常感谢@frank它正在工作