mysql - Node.js orm2 在循环中创建元素

标签 mysql node.js orm coffeescript

我的代码从数组中插入两次相同的值(最后一个)。为什么 ?如何插入多行但仅当它们不存在时?

        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

最佳答案

我认为您正在循环内对 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

关于mysql - Node.js orm2 在循环中创建元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17283468/

相关文章:

mysql - 我应该在大型 EAV 表中使用时间戳还是单独的时间列?

javascript - NodeJS 套接字数据拆分

mysql - 通过用户与城市关系查找国家/地区,其中用户与一个国家/地区的所有城市相关

mysql - 在同一个表中选择多个条件

mysql - 与mysql的uuid_short函数的值长度不同

javascript - Backbone save() 响应成功

node.js - sequelize postgres 批量插入忽略如果存在

django orm - 如何在其父类(super class)的子类的外键上使用 select_related()

java - @IdClass 与非原始@Id

php - 使用 bindParam 创建 MySQL 查询时出现“无效参数编号”错误