Search code examples
asynchronouseachsails.jswaterlinesails-postgresql

findOrCreate creates duplicates


I'm using oracle adapter, async.each and findOrCreate to transfer some data from oracle to my postgres db:

//simplified version
oracle.select(sql, [], function(err, results) {
    async.each(results, function(add_me, async_cb){
        model_to_add.findOrCreate(
            {not_id_field: add_me.value},
            {required_fields: add_me.values}
        ).exec(function add_me_cb(err, record){
            if (record && !err){
                async_cb();
            }
        });
    });
})

My sql query returns multiple, not unique values for not_id_field. But I want it to be unique in my postgres db. I thought finOrCreate is the great thing to use. But it somehow fails to find a record.

Was I wrong? Or maybe there's something I'm missing? sails.js documentation isn't really helpful : (


Solution

  • Turns out it was a problem with me - I didn't really understand how async.each works and that it is executed for every item in the array almost at the same time. I switched to async.eachSeries and now it works fine.