(慶祝我在 Stackoverflow 上的第一篇文章??)
我有一個個人專案,前端使用 React.js,后端使用 Node.js/express,我的資料庫是 mySQL。
我有這個陣列:
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é' }
]
我想用這些新聞值更新我的“horaires”表。該表如下所示:
---- ---------- -----------
| 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é |
---- ---------- -----------
我嘗試 TRUNCATE 然后 INSERT,我嘗試更新...我嘗試用 reducer 格式化我的陣列以在我的請求中使用它:
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é' ]
]
大多數時候我都有這個錯誤:你的 SQL 語法有錯誤;檢查與您的 MySQL 服務器版本相對應的手冊,以在第 1 行的 '0、'Lundi'、'Fermé'' 附近使用正確的語法。
如何將我的陣列格式化為 TRUNCATE 然后 INSERT ?或者用新值更新我當前的表?最佳做法是什么?
在此先感謝您的幫助...
uj5u.com熱心網友回復:
使用map(),不使用reduce()。
const newHoraires = horaires.map(({jour, horaire}, index) => [index, jour, horaire])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/491264.html
