簡體   English   中英

Firebase:推送到實時數據庫后立即刪除數據

[英]Firebase: data gets removed immediately after pushing to realtime database

這是第一次海報!

當我嘗試使用ReactJS和Firebase實時數據庫構建一個小練習組織器應用程序時,我遇到了Firebase push()方法的問題。

我的頁面上有幾個元素,一旦點擊它們就會將數據推送到數據庫,如下所示:

const planRef = firebase.database().ref("plan");
const currentExName = e.currentTarget.firstChild.textContent;
const exercise = {
     name: currentExName,
     type: e.currentTarget.children[1].textContent,
     user: this.state.user.displayName
};
planRef.push(exercise);

此外,如果再次單擊該元素,則會從數據庫中刪除它,如下所示:

planRef.orderByKey().on("value", snapshot => {
            let exercises = snapshot.val();
            for (let ex in exercises) {
                if (exercises[ex].name === currentExName) {
                    planRef.child(ex).set(null);
                }
            }
        });

只要我剛剛從數據庫中刪除了最后一點數據,我就不會嘗試將某些東西推送到數據庫中。 在那種情況下,它會立即被刪除。

數據被刪除

摘要:

  1. 使用ref.push()將數據寫入實時數據庫
  2. 使用ref.child(child).set(null)刪除數據remove()之前我試過remove() ,同樣的問題)
  3. 嘗試再次將相同的數據推送到數據庫,這會導致數據在寫入數據庫后立即被刪除

到目前為止,我找不到任何關於這類問題的內容,所以我猜我可能在某個地方犯了錯誤。 如果提供的信息不充分,請與我們聯系。

提前致謝。

刪除子進程是一種異步操作。 我想這里發生的事情是刪除操作比新的寫操作花費更多的時間。 如果要在同一個密鑰上再次寫入,則需要等待它。

planRef.child(ex).set(null).then(() => {
   planRef.child(ex).push(new);
});

或者使用async / await:

await planRef.child(ex).set(null);
planRef.child(ex).push(new);

讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM