我遇到的问题是将对象从Node.js传递到数据库(MySQL)。
当前,我正在使用INSERT方法,该方法仅允许我传递字符串,如果尝试传递对象,则会出错
这是我现在正在使用的:
let new_movie_data = `INSERT INTO testing (name, last) VALUES ('Jhonee','Mel')`
connection.query(new_movie_data, function (err, result){
if(err) throw (err)
})
这是我从react那里得到的,想要发送到数据库:
{
name:"Jhonee",
last:"Mel"
}
尝试将其放在“ VALUES”中将导致错误。
谢谢:D
答案 0 :(得分:0)
您可以将JSON解析为其组件:
jsonObj = JSON.parse(stringFromReact);
const new_movie_data = 'INSERT INTO testing (name, last) VALUES (?, ?);
connection.query(new_movie_data, [jsonObj.name, jsonObj.last], function (err, result){
if(err) throw (err)
});