Tengo un proyecto personal que usa React.js para el front-end, Node.js/express para el back-end y mi base de datos es mySQL.
Tengo esta matriz:
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é' } ]Y me gustaría actualizar mi tabla de "horaires" con estos nuevos valores. Esta tabla se ve así:
+----+----------+-----------+ | 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é | +----+----------+-----------+Traté de TRUNCAR y luego INSERTAR, traté de ACTUALIZAR... Traté de formatear mi matriz con reductor para usarla en mi solicitud:
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é' ] ]Tengo este error la mayor parte del tiempo: tiene un error en su sintaxis SQL; Consulte el manual que corresponde a la versión de su servidor MySQL para conocer la sintaxis correcta para usar cerca de '0, 'Lundi', 'Fermé' en la línea 1.
¿Cómo puedo formatear mi matriz para TRUNCAR y luego INSERTAR? ¿O ACTUALIZAR mi tabla actual con nuevos valores? ¿Cuál sería la mejor práctica?
Usa map() , no reduce() .
const newHoraires = horaires.map(({jour, horaire}, index) => [index, jour, horaire])