我是Coding和Firestore的新手,出于某种原因我无法正确使用它。
我想返回doc.id
,但是我得到的输出是 undefined ,似乎没有返回任何数据
console.log(doc.id)
的输出为hqyWHGrVHvopdyVACRkn
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)
}
}
答案 0 :(得分:0)
您以错误的方式应用了await
。如果将Promise
传递给await
表达式,它将等待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);
}