Javascript 检查firebase DB中是否存在项目,以及该项目是否存在';不存在,正在添加新的

Javascript 检查firebase DB中是否存在项目,以及该项目是否存在';不存在,正在添加新的,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我正在尝试在VueJS和Firebase中为我的计费系统创建一个函数。 不知何故,代码的else{…}部分没有运行,即使该项在数据库中不存在 var item for (item in this.items) { var a = this var s = this.items[item].Stock var sup = this.newStockSupplier var m = this.items[item].Model var exists =

我正在尝试在VueJS和Firebase中为我的计费系统创建一个函数。 不知何故,代码的else{…}部分没有运行,即使该项在数据库中不存在

  var item
  for (item in this.items) {
    var a = this
    var s = this.items[item].Stock
    var sup = this.newStockSupplier
    var m = this.items[item].Model
    var exists = false
    stockRef.orderByChild("Model").equalTo(m).once("value",snapshot => {
        if (snapshot.exists()){
          exists = true
        }
    });
    console.log(exists)
    if (exists = true){
      stockRef.orderByChild("Model").equalTo(this.items[item].Model).on("child_added", function(snapshot) {
        console.log('Item exists in DB')
          var stockItemRef = db.ref('stock/' + snapshot.key + '/Stock')
          stockItemRef.transaction(function(currentStock) {
            return currentStock + s
          })
          console.log('Updated Stock.')
      })
    }
    else {
      console.log("Item doesn't exist in DB")
      var newItem = new Object()
      newItem.Model = a.items[item].Model
      newItem.Stock = a.items[item].Stock
      newItem.Supplier = a.newStockSupplier
      stockRef.push(newItem)
      console.log('Added new product')
    }

  }
我使用两个独立的引用实例尝试了另一种方法,但不知何故它运行了两次代码:

stockRef.orderByChild("Model").equalTo(this.items[item].Model).on(  "child_added", function(snapshot) {
      if (snapshot.val() !== null) {
        var stockItemRef = db.ref('stock/' + snapshot.key + '/Stock')
        stockItemRef.transaction(function(currentStock) {
          return currentStock + s
        })
        console.log('Updated Stock.')
      } 
    })
    stockRef.orderByChild("Model").equalTo(this.items[item].Model).on("value", function(snapshot) {
      if (snapshot.val() === null) {
        // add code here to create new field
        var newItem = new Object()
        newItem.Model = this.items[item].Model
        newItem.Stock = this.items[item].Stock
        newItem.Supplier = this.newStockSupplier
        console.log(newItem)
        stockRef.push(newItem)
        console.log('Added new product')
      }

    })
  }

问题在于
stockRef.orderByChild(“Model”).equalTo(m).once()
方法是异步的,这意味着在触发方法的回调之前,代码的
exists=true
部分不会执行

另一个冲突是,在检查真实性时,将
true
赋值给
exists
变量。请记住,要进行比较,可以使用
=
==
运算符

您可以尝试使用以下方法:

    var item
    for (item in this.items) {
        var a = this
        var s = this.items[item].Stock
        var sup = this.newStockSupplier
        var m = this.items[item].Model
        var exists = false
        stockRef.orderByChild("Model").equalTo(m).once("value", snapshot => {
            // Declare your code inside the callback function
            if (snapshot.exists()) {
                stockRef.orderByChild("Model").equalTo(this.items[item].Model).on("child_added", function (snapshot) {
                    console.log('Item exists in DB')
                    var stockItemRef = db.ref('stock/' + snapshot.key + '/Stock')
                    stockItemRef.transaction(function (currentStock) {
                        return currentStock + s
                    })
                    console.log('Updated Stock.')
                })
            } else {
                console.log("Item doesn't exist in DB")
                var newItem = new Object()
                newItem.Model = a.items[item].Model
                newItem.Stock = a.items[item].Stock
                newItem.Supplier = a.newStockSupplier
                stockRef.push(newItem)
                console.log('Added new product')
            }
        });
    }

问题在于
stockRef.orderByChild(“Model”).equalTo(m).once()
方法是异步的,这意味着在触发方法的回调之前,代码的
exists=true
部分不会执行

另一个冲突是,在检查真实性时,将
true
赋值给
exists
变量。请记住,要进行比较,可以使用
=
==
运算符

您可以尝试使用以下方法:

    var item
    for (item in this.items) {
        var a = this
        var s = this.items[item].Stock
        var sup = this.newStockSupplier
        var m = this.items[item].Model
        var exists = false
        stockRef.orderByChild("Model").equalTo(m).once("value", snapshot => {
            // Declare your code inside the callback function
            if (snapshot.exists()) {
                stockRef.orderByChild("Model").equalTo(this.items[item].Model).on("child_added", function (snapshot) {
                    console.log('Item exists in DB')
                    var stockItemRef = db.ref('stock/' + snapshot.key + '/Stock')
                    stockItemRef.transaction(function (currentStock) {
                        return currentStock + s
                    })
                    console.log('Updated Stock.')
                })
            } else {
                console.log("Item doesn't exist in DB")
                var newItem = new Object()
                newItem.Model = a.items[item].Model
                newItem.Stock = a.items[item].Stock
                newItem.Supplier = a.newStockSupplier
                stockRef.push(newItem)
                console.log('Added new product')
            }
        });
    }