问题描述
我有这个INSERT查询。它的确在表中创建了一条记录,但是“ client”的值为0,而“ short”的值为null。但是,所有控制台日志都显示正确的数据。我假设这是我使用问号时的简单语法错误。但是我找不到它,或者似乎也只能使用变量名来使其工作。任何帮助表示赞赏。
app.put("/audit/create",function (req,res) {
console.log("It is getting to the route");
const client = req.body.client;
const short = req.body.short;
console.log(`client: ${client}`);
console.log(`short: ${short}`);
connection.getConnection(function (err,connection) {
connection.query(
`INSERT INTO audits (client,short)
VALUES (?,?)`,[
{
client: client,},{
short: short,],function (error,results,fields) {
if (error) throw error;
res.json(results);
console.log(`Audit has been created`);
}
);
connection.release();
});
});
我还尝试使用$ {client},$ {short}变量名代替问号...,但这仍然给我相同的结果。但是,呈现$ {client}和$ {short}的控制台日志确实记录了我期望看到的正确数据。
解决方法
为什么要将对象作为值传递给查询?尝试一下:
const data = [{"uid":"j5Dc9Z","options":[{"name":"Transportation","checked":true},{"name":"Medication","checked":true}]},{"uid":"a5gdfS","options":[{"name":"Food supply",{"uid":"jHab6i","options":[{"name":"Package distri",{"name":"maintainence","checked":false}]}]
const result = data.flatMap(o => o.options)
.filter(o => o.checked)
console.log(result)