我的代码从数组中插入两次相同的值(最后一个)。为什么?如何插入多行,但只有它们不存在?
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
答案 0 :(得分: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