我正在迭代一个数组,我正在使用findOrCreate
方法添加新记录,我希望在结尾处显示 所有 记录行动......这就是我所拥有的:
var counter = 2000
for (var i = 0; i < arr.length; i++) {
var line_array = arr[i].split(":")
var adapter_id = transponder.adapter_id
var transponder_id = transponder.id
var port = counter
var service_name = line_array[0]
var frequency = line_array[1]
var polarization = line_array[2]
var device = line_array[3]
var symbol_rate = line_array[4]
var video_id = line_array[5]
var audio_id = line_array[5]
var service_id = line_array[7]
Channel.findOrCreate({
frequency: parseInt(frequency),
symbol_rate: parseInt(symbol_rate),
service_id: parseInt(service_id)
}, {
adapter_id: adapter_id,
transponder_id: transponder_id,
port: port,
service_name: service_name,
frequency: frequency,
polarization: polarization,
device: device,
symbol_rate: symbol_rate,
video_id: video_id,
audio_id: audio_id,
service_id: service_id,
}).exec(function(err, created) {
console.log(i)
//console.log(created.service_id)
})
counter++
}
Channel.find({
transponder_id: req.param("transponder_id")
}).exec(function(err, channels) {
res.view("transponders/channel_scan", {
transponder: transponder,
channels: channels
});
})
当我运行时,我没有任何错误。尽管如此,我只能获得空的或更旧的记录......而不是新记录。我猜这背后的原因是Waterline查询是异步的,我的动作是在这些查询返回值之前执行的。
但问题是我将如何处理?我已经阅读了一些关于承诺但却无法实现的内容。一些帮助会很棒。
谢谢!