Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

531
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda