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

128
Visualizações
Problem to updating mysql table with array of object js

I have a personal project using React.js for front-end, Node.js/express for back-end and my database is mySQL.

I have this array :

horaires = [
  { jour: 'Lundi', horaire: 'Fermé' },
  { jour: 'Mardi', horaire: 'Fermé' },
  { jour: 'Mercredi', horaire: 'Fermé' },
  { jour: 'Jeudi', horaire: 'Fermé' },
  { jour: 'Vendredi', horaire: 'Fermé' },
  { jour: 'Samedi', horaire: 'Fermé' },
  { jour: 'Dimanche', horaire: 'Fermé' }
]

And I would like to update my "horaires" table with these news values. This table looks like this :

+----+----------+-----------+
| id | jour     | horaire   |
+----+----------+-----------+
|  1 | Lundi    | Fermé     |
|  2 | Mardi    | 18h - 21h |
|  3 | Mercredi | 18h - 21h |
|  4 | Jeudi    | 18h - 21h |
|  5 | Vendredi | 18h - 21h |
|  6 | Samedi   | 18h - 21h |
|  7 | Dimanche | Fermé     |
+----+----------+-----------+

I tried to TRUNCATE then INSERT, I tried to UPDATE... I tried to formate my array with reducer to use it in my request :

  const newHoraires = horaires.reduce((acc, current, index) => {
    const newArray = [];
    newArray.push(index);
    newArray.push(current.jour);
    newArray.push(current.horaire);
    acc.push(newArray);
    return acc;
  }, []);

// Output => newHoraires [
  [ 0, 'Lundi', 'Fermé' ],
  [ 1, 'Mardi', 'Fermé' ],
  [ 2, 'Mercredi', 'Fermé' ],
  [ 3, 'Jeudi', 'Fermé' ],
  [ 4, 'Vendredi', 'Fermé' ],
  [ 5, 'Samedi', 'Fermé' ],
  [ 6, 'Dimanche', 'Fermé' ]
]

I have this error most of the time : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 'Lundi', 'Fermé'' at line 1.

How can I formate my array to TRUNCATE and then INSERT ? Or UPDATE my current table with new values? What would be the best practice?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Use map(), not reduce().

const newHoraires = horaires.map(({jour, horaire}, index) => [index, jour, horaire])
about 4 years ago · Santiago Gelvez 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