0

I'm iterating over an array, I'm using findOrCreate method to add new records and I want show all records at the end of the action... Here is what I have:

          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
            });

          })

When I run this, I don't get any errors. Nevertheless, I get empty or older records only... Not new ones. I can guess the reason behind this is Waterline queries are asynchronous, my action is executed before those queries return a value.

But the question is how will I deal with it? I've read some stuff about promises but couldn't implement. Some help would be great.

Thank you!

scaryguy
  • 7,720
  • 3
  • 36
  • 52

0 Answers0