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
I think you are making an async call to MovieSpecies.exists within the loop. When you want to loop over a list and do async calls I do something like this:
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