0
我想列出哈希標籤集合中的新哈希標籤(例如#love,#goodMorning)(如果它們不存在) 此外,應用程序還支持任何登錄用戶可以寫入數據庫。如果不存在(同步),在谷歌firestore中添加數據
如果只有一個用戶有權訪問此文檔,則可以使用以下代碼。
hashtags=["one","two","three"];
hashtags.forEach(hashtagToAdd => {
this.afs.collection("hashtags").ref
.where("name", "==", hashtagToAdd)
.limit(1)
.get()
.then(result => {
if (result.empty) {
// code to add new hashtag e.g. here hashtagToAdd
} else {
console.log("hashtag is already present");
}
})
.catch(error => {
console.log("error aayo che", error);
});
}
在我的情況,因爲多個用戶可以訪問和寫入它。 現在,如果在取得標籤之前沒有找到這樣的標籤,但是在添加標籤之前,另一個用戶如果已經添加了相同的標籤,那麼可以存在重複的標籤。 如何避免這種情況?還有沒有更好的方法來做到這一點的新標籤列表?