Mr. BigglesWorth Mr. BigglesWorth - 1 month ago 11x
Node.js Question

Looping through objects and adding them to Sequelize db

I have an array of objects that I like to put into SQL via Sequelize and I'm running into issues.

{ owner: false,
id: '2342365',
name: 'awrAPI' },
{ owner: false,
id: '5675689699',
name: 'TgatAPI' },
{ owner: true,
id: '57456767',
name: 'ytasyAPI' }

Currently the way i have it set up is through a simple for in.

for( var key in guilds ) {
.findOrCreate({where: {primid: guilds[key].id}, defaults: {
primid: guilds[key].id,
name: guilds[key].name,
owner: guilds[key].icon
.spread(function(guild, created) {
plain: true

I assume this is wrong and was wondering if there is a better way to loop through my object and chain the findorcreates. Currently it goes through the first object, but then does not add any more. I've been looking into using Bluebird, but I'm not too familiar with using promises. Thoughts?

Promise.all( {
  // Do your thing
  return Guild.findOrCreate...
})).then(function() {
  // All is resolved do your next thing