Node + Sequelize: How to check if item exists before adding? (async confusion) Node + Sequelize: How to check if item exists before adding? (async confusion) node.js node.js

Node + Sequelize: How to check if item exists before adding? (async confusion)


Author.count isn't really needed, unless you need the count. See findOrCreate().

With findOrCreate() you could have the following. (Edited trex005's snippet for this)

async.eachSeries(authors, function(item, callback) {  Author.sync().then(function() {    Author.findOrCreate({      where: {        name: item.trim()      },      defaults: { // set the default properties if it doesn't exist        name: item.trim()      }    }).then(function(result) {      var author = result[0], // the instance of the author        created = result[1]; // boolean stating if it was created or not      if (!created) { // false if author already exists and was not created.        console.log('Author already exists');      }      console.log('Created author...');      callback();    });  })})