我在sails.js中遇到异常事件的问题。
我正在从JSONapi获取一些数据并尝试将它们写入for循环中的数据库中。一切都需要一个接一个地执行(按照正确的顺序)。为了使示例简单,我只想说我正在尝试执行以下操作:
//let apiNames be a JSON that contains some names in that order: James, Betty, Jon
//let Customer be a DB which contains that names and related age
for(index in apiNames){
console.log("Searching for "+apiNames[index].name+" in the Database...);
Customer.find({name:apiNames[index].name}).exec(function(err,customerData){
console.log("Found "+apiNames[index].name+" in the Database!");
console.log(customerData);
});
}
建议日志应如下所示:
Searching for James in the Database....
Found James in Database!
{name:James, age:23}
Searching for Betty in the Database....
Found Betty in Database!
{name:Betty, age:43}
Searching for Jon in the Database....
Found Jon in Database!
{name:Jon, age:36}
由于Javascript以异步方式运行且数据库调用时间很长,因此输出看起来像这样:
Searching for James in the Database....
Searching for Betty in the Database....
Searching for Jon in the Database....
Found James in Database!
{name:James, age:23}
Found Betty in Database!
{name:Betty, age:43}
Found Jon in Database!
{name:Jon, age:36}
我已经尝试了几种方法来强制循环同步工作,但没有任何效果。 AFAIK如果我在exec中调用某些内容,它应该同步运行(通过链接它与另一个exec),但我的问题是,它已经失败了在循环中同步工作。有没有人有这个解决方案,可以解释一下?
编辑:apiNames不是一个数组,它是一个JSON,里面有一些数据。以下是apiNames的示例:
[{
"name": "James",
"gender": "male"
},
{
"name": "Betty",
"gender": "female"
},
{
"name": "Jon",
"gender": "male"
}]
(添加了性别以在json中获得更多信息。它对解决方案不重要)
答案 0 :(得分:3)
由于apiNames是一个对象,对于IE9 +兼容性,我们可以使用Object.keys()来获取对象中的键名,并使用它来遍历apiNames
//process all names in the array one by one
function process(apiNames, keys) {
//if there are no items in the array return from the function
if (!keys.length) {
return;
}
//get the first name in the array
var key = keys.shift();
var name = apiNames[key];
console.log("Searching for " + name + " in the Database...");
Customer.find({
name: name
}).exec(function (err, customerData) {
console.log("Found " + name + " in the Database!");
console.log(customerData);
//once the current item is processed call the next process method so that the second item can be processed
process(apiNames, keys);
});
}
//call the process method with an array
var keys = Object.keys(apiNames);
process(apiNames, keys);
对于旧版浏览器,使用polyfill添加对Object.keys()的支持,如one provided by MDN
演示:Fiddle
答案 1 :(得分:0)
您可以使用递归来测试布尔值,当您准备就绪时可以切换。
var isFinding = false;
var found = [];
for (index in apiNames) {
checkIndex(index);
}
function checkIndex (index) {
if (index) {
found[index - 1]
? findCustomer(index)
: setTimeout(checkIndex, 100, index);
} else {
findCustomer(index);
}
}
function findCustomer (index) {
if (isFinding) {
setTimeout(findCustomer, 100, index)
} else {
isFinding = true;
console.log("Searching for "+apiNames[index]+" in the Database...");
Customer.find({name:apiNames[index]}).exec(function(err,customerData){
console.log("Found "+apiNames[index]+" in the Database!");
console.log(customerData);
found[index] = true;
isFinding = false;
});
}
}
console.log("Searching for "+apiNames[index]+" in the Database...");
这是未经测试的,我半睡半醒......但我很确定这应该如你所愿......或者至少让你接近: - )
答案 2 :(得分:0)
因为我相信我们在这里讨论nodejs,所以我会使用async.js
和apiNames对我来说就像是阵列,如果不是那么就是
var async = require('async');
var apiNames = JSON.parse('[{"name": "James","gender": "male"},{"name": "Betty","gender": "female"}]');
async.eachSeries(apiNames, function(apiName, callback) {
console.log("Searching for "+apiName.name+" in the Database...);
Customer.find({name:apiName.name}).exec(function(err,customerData){
console.log("Found "+apiName.name+" in the Database!");
console.log(customerData);
callback();
});
});