簡體   English   中英

如何在nodeJS中將map object添加到mysql?

[英]How to add a map object to mysql in nodeJS?

It's my first time creating a map object and I'm trying to add it to a mysql database but I have an error that says: "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the在第 1 行的 ' key = '#42343731'' 附近使用正確的語法"

map object 中的元素示例如下:

Map {
'#44928649' => {
  id: '#44928649',
  name: '508',
  year: '2020',
  price: 34800,
  included_tax: true,
  state: true
},
 '#44899990' => {
 id: '#44899990',
 name: 'yaris',
 year: '2018',
 price: 17800,
 included_tax: true,
 state: true
 }
}

我的查詢如下:

 function addNewElement(value, key, map){
           connection.query("INSERT INTO test(id, name, year, price, included_tax, status) VALUES ?", {key}, (err, res) => {
                if (err) throw err;
                console.log("new element: ", res.insertId)
            });
 }
 carsList.forEach(addNewElement);

我認為我在查詢中添加元素的方式不正確。 我的另一個問題是,您是否建議將數組保留在 map 的每個鍵的值內,還是您認為最好也制作它們的映射?

謝謝!

{key}返回您定義的 map 的密鑰。 即“#44928649”或“#44899990”。

這意味着您的 SQL 語句如下所示:
INSERT INTO test(id, name, year, price, included_tax, status) VALUES '#44928649'

您需要做的是返回 map 中的鍵的值

嘗試以下操作:

connection.query("INSERT INTO test (id, name, year, price, included_tax, status) SET ?", value, (err, res)...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM