我正在尝试更新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)
);
}
请帮助我
答案 0 :(得分:0)
我也有同样的问题,我以这种方式尝试了代码:
System.out.println(datetimeString);
System.out.println(odt);
System.out.println(odtUTC);
答案 1 :(得分:0)
我通过使用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)
);
}
答案 2 :(得分:0)
听起来您有两项操作:
从Firestore读取数据或将数据写入Firestore的代码异步运行。为了防止阻塞应用程序,在后台运行读/写操作时,允许您继续正常的代码流。这意味着查询/读取当前正在您的代码中运行,写入操作尚未完成,因此您在查询中看不到该写入操作的结果。
解决方案是等到写操作完成后再开始查询。您已经在查询中执行了此操作,在该查询中使用.then(snapshot => {
构造来等待结果。您需要对update(...)
调用执行相同的操作,例如:
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));
})
})
}
主要变化如下:
update
数组中捕获promises
调用的结果。这为我们提供了一个承诺列表,这些列表将在写操作完成时解决。Promise.all()
等待所有写操作完成,然后再开始查询。