[英]Ionic firestore how to check document id exist in firestore or not
[英]Firestore - How to check if a document exist based on a specific field
我想要實現的是根據像liker_id這樣的字段檢查文檔是否存在,如果存在則返回,如果不存在則使用liker_id創建一個新文檔。
我已經嘗試過 snapshot.exists 和 doc.exists 但它們似乎給出了奇怪的行為,其中 console.log 沒有被觸發,或者集合仍然添加文檔,即使它已經存在。
const liker = db
.collection("post")
.doc('ubxL0nDCLHgrtEyQ5TEV')
.collection("votes")
.where("liker_id", "==", "user-TVe8mMRU47cnfO4xbl9yQEEE4XB3")
.get()
.then(snapshot => {
snapshot.forEach(doc => {
if (doc.exists) {
console.log("exist");
} else {
console.log("not exist");
}
});
})
.catch(error => {
console.log(error);
});
你會嘗試這樣的代碼嗎?
const liker = db
.collection("post")
.doc('ubxL0nDCLHgrtEyQ5TEV')
.collection("votes")
.where("liker_id", "==", "user-TVe8mMRU47cnfO4xbl9yQEEE4XB3")
.get()
.then(snapshot => {
const data = snapshot.val();
data.forEach(doc => {
if (doc.exists) {
console.log("exist");
} else {
console.log("not exist");
}
});
})
.catch(error => {
console.log(error);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.