Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

529
Views
Bulk Insert into Mysql from Object Array NodeJS

I'm not sure what I'm missing, I thought putting Games in brackets would let me do bulk insert to MySQL. However when I do this it comes back as (OBJECT,OBJECT). If I remove the brackets then I only get one single insert but it works! Any suggestions to do bulk insert?

Short sample of the Array

[{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx},{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}]

Javascript

// Writing the Games inside a json file
fs.writeFile("feed.json", JSON.stringify(Games), function(err) {
  if (err) throw err;
  console.log("Saved!");
});

 con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
  var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
  let queryData = {
    sql: sqlquery,
    values: [Games]
  }
  con.query(queryData, function (err, result) {
    if (err) throw err;
    console.log("Number of records inserted: " + result.affectedRows);
  });
});
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I believe your issue may be that you are passing an array of objects while MySQL is expecting an array of arrays.

Your format is basically:

var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
var values = [
[{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}
{title:xxx,link:https://xxxx,description:xxx.,xxx:xxxxx}]
];

When it should be:

var sqlquery  = "INSERT INTO gtable (title, link, description, company) VALUES ?";
var values = [
[[title1,link1,description1,xxx1],
[title2,link2,description2,xxx2]]
];

You would need to convert each object to an array of just its values. Which you could do with something like this I think:

let sqlValues = Games.map(obj => Object.values(obj));

EDIT: I had the incorrect format for the mapping, fixed now.

over 4 years ago · Santiago Trujillo Report

0

I believe @otw has a solid solution (and an awesome explanation of why this is happening), but to offer an alternative solution you could do something like this:

Games.forEach(game => {
  con.query('INSERT INTO gtable SET ?', game, insertErr => {
    if (insertErr) {
      throw new Error("Error inserting game " + game.title + "! " + insertErr);
    }
  
    console.log(`Inserted ${game.title}!`);
  });
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!