Node.js Firestore更新文档字段和获取IMMIDATE响应问题获取旧数据
我正在尝试更新firestore字段值。它正在更新,但我想立即使用更新的文档。当我当时获取这些数据时,它会在我第一次访问api时提供来自Firestore的旧数据。如果我两次点击相同的api,那么我会得到更新的数据 所以,我不明白真正的问题是什么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
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)
);
}
听起来您有两个操作:
.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
调用的结果。这为我们提供了一个promises列表,在写入操作完成时解析update
- 然后,它使用
等待所有写入操作完成,然后再开始查询Promise.all()
。然后(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
调用的结果。这为我们提供了一个promises列表,在写入操作完成时解析update
- 然后,它使用
等待所有写入操作完成,然后再开始查询Promise.all()