【发布时间】:2022-01-31 13:26:25
【问题描述】:
想法
我制作了第二张表,用于存储有关客户的特殊信息。 customer_Id 是我的客户表的外键。但是,根据我的反应,我有一个表格,根据该表格中的信息,我需要发送不同的查询。我尝试了一个 if 语句,它使用正确的信息将该行添加到表中,但它没有将 newReq 设置为返回的内容。当我将它移到 if 语句之外时,它可以正常工作并返回正确的信息。是否有解决此问题的方法,或者我该如何以更好的方式进行此操作?
try {
if (req.body.type === "allergy") {
console.log(req.params.id)
const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,allergicto,dangerscale,priority) values($1,$2,$3,$4,$5) returning *',[req.params.id, req.body.type, req.body.allergicto, req.body.dangerscale, req.body.priority]);
}
else if (req.body.type === "scaredof") {
const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,freight,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.freight, req.body.priority]);
}
else if (req.body.type === "medicine") {
const newReq = await db.query('INSERT INTO specialcustomerinfo(customer_id,type,medicineName,priority) values($1,$2,$3,$4) returning *',[req.params.id, req.body.type, req.body.medicineName, req.body.priority]);
}
else {
console.log("No type match")
}
res.status(201).json({
status: "success",
data: {
bonus: newReq.rows[0],
},
});
} catch (err) { console.log(err) }
})```
【问题讨论】:
标签: node.js reactjs postgresql express