Javascript 返回快照firestore JS中的文档ID
我对编码和Firestore还不熟悉,因为某些原因,我不能正确使用它。 我想返回Javascript 返回快照firestore JS中的文档ID,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我对编码和Firestore还不熟悉,因为某些原因,我不能正确使用它。 我想返回doc.id,但我得到的输出是未定义的,似乎没有返回任何数据 控制台日志(doc.id)的输出是 ID的输出是未定义的 const getProductDocId = async (productName1) =>{ try{ const ID = await firestore().collection('product').where('productName' ,'==', productNa
doc.id
,但我得到的输出是未定义的,似乎没有返回任何数据
控制台日志(doc.id)的输出是
ID
的输出是未定义的
const getProductDocId = async (productName1) =>{
try{
const ID = await firestore().collection('product').where('productName' ,'==', productName1).get().then(snapshot=>{
snapshot.docs.every(doc => {
console.log(doc.id + " THIS is DOC.ID")
return doc.id
})
})
console.log(ID)
}catch(error){
console.log("Error @ Get Prdocut ID " + error)
}finally{
console.log(ID)
}
}
您以错误的方式应用了
等待
。如果Promise
被传递给wait
表达式,它将等待Promise
得到满足,并返回满足的值
try {
const snapshot = await firestore()
.collection("product")
.where("productName", "==", productName1)
.get();
let id = null;
snapshot.docs.every((doc) => {
console.log(doc.id + " THIS is DOC.ID");
id = doc.id;
});
console.log(id);
} catch (error) {
console.log("Error @ Get Prdocut ID " + error);
} finally {
console.log(id);
}