0

My code inserts two times the same values from array (last one). Why ? How to insert many rows but only if they doesn't exists ?

        for x of species # two elements 36 and 37
            data = { movie_id : id, species_id : species[x].id }
            console.log data
            MovieSpecies.exists data, (err, exists) ->
                if exists == false
                    MovieSpecies.create data, (err, items) ->
                        console.log items
4

1 回答 1

0

我认为您正在循环内对 MovieSpecies.exists 进行异步调用。当您想遍历列表并进行异步调用时,我会执行以下操作:

urls = ['http://cnn.com', 'http://cnet.com']

do_loop = (index) ->
  if index == urls.length
    alert 'all done!'
  else
    http_get urls[index], (result) ->
      do_loop index+1

do_loop 0
于 2013-06-25T22:47:53.587 回答