IDEA
I have made a second table that I will store special information about a customer. The customer_Id is a foreign key to my customer table. However from my react I have a form and depending on the information in that form I need to send a different query. I have tried with a if sentence and it adds the row to the table with correct information but it doesn't set newReq to what is being returned. When I move it outside the if sentence it works fine and returns the correct info. Is there a fix to this or how can I go about doing this in a better way maybe?
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) }
})```
CodePudding user response:
const and let are block-scoped in Javascript. That means they can only be accessed inside the block in which they are declared. So, all the different versions of const newReq you declare can only be used inside their block. Instead, declare the variable using let at a higher level in the function so you can use it where you want to.
try {
let newReq;
if (req.body.type === "allergy") {
console.log(req.params.id)
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") {
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") {
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.sendStatus(401);
return;
}
res.status(201).json({
status: "success",
data: {
bonus: newReq.rows[0],
},
});
} catch (err) {
console.log(err);
res.sendStatus(500);
}
})
FYI, you also need to add a fix for when the code goes into your else clause because that will throw when you try to reference newReg.rows. Probably, you should just send some error status in the else clause and then return which is what I show.
You also need to send an error response in your catch since all paths through your function need to send some sort of response.
