1

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 ) {
        Guild
        .findOrCreate({where: {primid: guilds[key].id}, defaults: {
            primid: guilds[key].id,
            name: guilds[key].name,
            owner: guilds[key].icon
        }})
        .spread(function(guild, created) {
            console.log(guild.get({
            plain: true
            }))
            console.log(created)
        })                      
    }

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?

4
  • What are your "issues"? Commented Sep 19, 2016 at 5:49
  • @DanielB "Currently it goes through the first object, but then does not add any more". I also assume that this shouldn't be the way to handle it and more than likely requires some use of promises. I'm just not sure Commented Sep 19, 2016 at 5:51
  • I suppose, your code throws 'guild is not defined' error, because your spread function recieve first parameter named as user but then call function of guild instance that isn't defined, they should have same name. Try to change first parameter of spread function to 'guild' Commented Sep 19, 2016 at 9:54
  • @TilovYrys Actually that's just a typo. It is named guild in the actual code Im running. Commented Sep 19, 2016 at 18:39

2 Answers 2

3
var myData = [ 
  { 
    owner: false,
    id: '2342365',
    name: 'awrAPI' 
  },
  { 
    owner: false,
    id: '5675689699',
    name: 'TgatAPI'
  },
  { 
    owner: true,
    id: '57456767',
    name: 'ytasyAPI'
  }
];

Promise.all(myData.map(function(value) {
  // Do your thing
  return Guild.findOrCreate...
})).then(function() {
  // All is resolved do your next thing
})
Sign up to request clarification or add additional context in comments.

2 Comments

Excuse my ignorance but I'm not as familiar as map as I'd like to be. I always assumed that .map only worked on Arrays and not objects.
True. On the question you hava an array, it was a typo on my answer. Fixed
2

An alternative would be to utilize squelize's bulkCreate model method for multiple records insertion. link to sequelize's bulkCreate docs

quick snippet here:

 const dataForInsertion = [ 
    {
      username:"daniel",
      currentORM:"Sequelize"
    },
    {
      username:"lekan",
      currentORM:"Mongoose"
    },
 ]

 Guild.bulkCreate(dataForInsertion)
     .then(()=>{})
     .catch(()=>{})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.