我想通过节点js将一些数据添加到firebase的项目列表中,但是我不知道如何检查更新过程是否失败。这是我的工具:
// set data to json
let servicesJson = {
"car_services": [services],
"car_services_status": 1
}
// add car service to province
let provinceIdNumber = parseInt(provinceId, 10);
var areaRef = db.ref("areas")
areaRef.orderByChild('province_id').equalTo(provinceIdNumber).once('value', function (areaSnapshots) {
if (areaSnapshots.val() != null) {
var result = true
areaSnapshots.forEach (function (child) {
let currentTime = new Date().toISOString();
servicesJson.last_updated = currentTime
child.ref.update(servicesJson, function (error) {
if (error) {
console.log("*** error ***")
result = false
} else {
console.log("*** success ***")
result = true
}
})
});
console.log("*** finished ***")
res.send({ 'success': result });
} else {
res.send({ 'success': false, 'error_code': 'CSTP05', 'error_msg': 'Cant find any area of province to update car service' });
}
});
这是日志:
*** finished ***
*** success ***
*** success ***
*** success ***
*** success ***
如何等待直到循环结束然后返回结果?
答案 0 :(得分:2)
Use Promise
// set data to json
let servicesJson = {
car_services: [services],
car_services_status: 1
};
// add car service to province
let provinceIdNumber = parseInt(provinceId, 10);
var areaRef = db.ref("areas");
const updatePromise = new Promise((resolve, reject) => {
const changedCount = 0;
areaRef
.orderByChild("province_id")
.equalTo(provinceIdNumber)
.once("value", function(areaSnapshots) {
if (areaSnapshots.val() != null) {
areaSnapshots.forEach(function(child) {
let currentTime = new Date().toISOString();
servicesJson.last_updated = currentTime;
child.ref.update(servicesJson, function(error) {
if (error) {
changedCount++;
console.log("*** error ***");
} else {
changedCount++;
console.log("*** success ***");
}
});
if (areaSnapshots.length === changedCount) {
console.log("*** finished ***");
resolve({ success: true });
}
});
} else {
reject({
success: false,
error_code: "CSTP05",
error_msg: "Cant find any area of province to update car service"
});
}
});
});
updatePromise
.then(data => {
res.send(data);
})
.catch(error => {
res.send(error);
});
答案 1 :(得分:1)
感谢HNQ的回答,这是有效的代码:
var updatePromise = new Promise((resolve, reject) => {
var changedCount = 0;
var isError = false;
areaRef
.orderByChild("province_id")
.equalTo(provinceIdNumber)
.once("value", function(areaSnapshots) {
if (areaSnapshots.val() != null) {
areaSnapshots.forEach(function(child) {
let currentTime = new Date().toISOString();
servicesJson.last_updated = currentTime;
child.ref.update(servicesJson, function(error) {
if (error) {
isError = true;
console.log("*** error ***");
} else {
console.log("*** success ***");
}
changedCount++;
if (areaSnapshots.numChildren() === changedCount) {
console.log("*** finished ***");
if (isError) {
resolve({ success: false, error_code: 'CSTP05', error_msg: 'Failed to update all elements' });
} else {
resolve({ success: true });
}
}
});
});
} else {
reject({
success: false,
error_code: 'CSTP06',
error_msg: 'Cant find any area of province to update car service'
});
}
});
});
updatePromise
.then(data => {
res.send(data);
})
.catch(error => {
res.send(error);
});
我添加了一个用于检测错误的标志。如果一切正常,结果将如下所示:
*** success ***
*** success ***
*** success ***
*** success ***
*** finished ***