亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何避免 Firebase / Cloud Firestore 中的重復數據

如何避免 Firebase / Cloud Firestore 中的重復數據

絕地無雙 2022-05-26 17:45:13
我正在制作一個網頁,但是當輸入相同的 ID 時應該會出現錯誤,但我無法做到。 function save () {    if (validate  = true) {        console.log("exists!")    }else {            var imei = document.getElementById('imei').value;    var marca = document.getElementById('marca').value;    var referencia = document.getElementById('referencia').value;    var precio = document.getElementById('precio').value;    db.collection("phone").add({        Imei: imei,        Marca: marca,        Referencia: referencia,        Precio: precio    })        .then(function (docRef) {            document.getElementById('imei').value = '';            document.getElementById('marca').value = '';            document.getElementById('referencia').value = '';            document.getElementById('precio').value = '';        })        .catch(function (error) {            window.alert("Error adding document: ", error);        });    }}save();function validate () {    firebase.database().ref(`phone/${Imei}/imei`).once("value", snapshot =>     { const imei = snapshot.val();         if (imei){             console.log("user exists!");         }     }); }如果您能告訴我哪里有錯誤,或者最好的解決方案,我將不勝感激
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您的代碼存在一些問題:

  1. 您正在使用 構建路徑phone/${Imei}/imei,但您的變量名是拼寫的imei(而不是Imei)。與大多數編程語言一樣,JavaScript 中的大小寫很重要,因此我建議密切注意拼寫和大小寫。

  2. 你沒有validate()在任何地方打電話,這意味著你的支票沒有運行。

  3. 你沒有從validate(). 而且由于您要返回的內容來自異步調用中的數據庫,因此您只能使用 promise 或async/來返回它await。這個 aslome 可以保證自己的答案,所以我建議你研究Firebase,異步檢索數據如何使用函數中的 async-await 從異步函數返回值?以及如何從異步回調函數返回值?

  4. 您確實應該使用事務來確保沒有人可以在代碼中的讀寫操作之間聲明 IMEI。

  5. 如果 IMEI 值應該是唯一的,最好將其用作而不是屬性值。在此處閱讀更多信息:

結合所有這些,更好的實現可能類似于:

function save () {

    var imei = document.getElementById('imei').value;

    var marca = document.getElementById('marca').value;

    var referencia = document.getElementById('referencia').value;

    var precio = document.getElementById('precio').value;


    var imeiDocRef = db.collection("phone").doc(imei);


    db.runTransaction(function(transaction) {

        // This code may get re-run multiple times if there are conflicts.

        return transaction.get(imeiDocRef).then(function(imeiDoc) {

            if (imeiDoc.exists) {

                throw `IMEI '${imei}' already exist!`;

            }


            transaction.set(imeiDocRef, { 

                Imei: imei,

                Marca: marca,

                Referencia: referencia,

                Precio: precio

            });

        });

    }).then(function() {

        console.log("Transaction successfully committed!");

        document.getElementById('imei').value = '';

        document.getElementById('marca').value = '';

        document.getElementById('referencia').value = '';

        document.getElementById('precio').value = '';

    }).catch(function(error) {

        console.log("Transaction failed: ", error);

    });

}


查看完整回答
反對 回復 2022-05-26
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號