gpt4 book ai didi

javascript - 检查 firebase DB 中是否存在某项,如果不存在,则添加新项

转载 作者:行者123 更新时间:2023-11-28 14:31:15 27 4
gpt4 key购买 nike

我正在尝试在 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')
}
});
}

关于javascript - 检查 firebase DB 中是否存在某项,如果不存在,则添加新项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51477415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com