Reputation: 71
I have two tables one for graduates and another for mentors. And I would like to find out which table an email is from using the following endpoint .
router.post("/reset/:email", async (req, res) => {
//search which table the email is from
const { email } = req.params;
try {
const result_g = await pool.query(
"select * from graduates where email=$1",
[email]
);
const result_m = await pool.query("select * from mentors where email=$1", [
email,
]);
if (result_g) {
return res.json({ id: result_g.rows[0].id, user: "graduates"});
} else if (result_m) {
return res.json({ id: result_m.rows[0].id, user: "mentors" });
} else {
return res.json("email isn't found");
}
} catch (err) {
return res.status(500).send(err, "server error");
}
});
However, I keep getting an error message "Unhandled promise rejection warning"
It isn't just a warning because I tried it using postman and the request doesn't work. I assume the error comes from the result_g and result_m or the if conditions but I cannot identify where exactly the problem is . I would really appreciate if someone explains why this isnt allowed in node/express. Thanks!
Upvotes: 1
Views: 119
Reputation: 6072
There's a few errors in this code. Unfortunately, they combine to make the Node.js error message really hard to read. But luckily, they're all easy fixes! Let's look identify them individually first, then pull them all together to explain the error.
We can't check whether rows were returned using if (result)
. The mysql query
function returns a ResultSet
, which will always be truthy. (if (result_g.rows)
doesn't work either – an empty array is truthy in JavaScript.)
So the if (result_g)
in your code always executes. The next line includes result_g.rows[0].i
. Which leads to...
Accessing a property on undefined
raises an exception. Even though our result set is empty – there are no matching graduates – we run return res.json({ id: result_g.rows[0].id, user: "graduates"})
.
result_g.rows
is an empty array []
.result_g.rows[0]
returns undefined
.result_g.rows[0]
raises a TypeError
("undefined is not an object").That error's caught by the catch
block, which brings us to the final problem.
The response syntax in the try/catch is incorrect. You try to send an error response via return res.status(500).send(err, "server error")
, but send()
takes only one parameter. (Older versions of Express have a 2-argument send()
function, but I don't think they support receiving an exception either.)
This raises another exception from within your catch
block. And that gives the error you see on your console: we threw an error within the async function of your router.post
definition.
Fortunately, we can fix these with some small edits:
try {
const result_g = await pool.query(
"select * from graduates where email=$1",
[email]
);
const result_m = await pool.query("select * from mentors where email=$1", [
email,
]);
- if (result_g) {
+ if (result_g.rows.length > 0) {
return res.json({ id: result_g.rows[0].id, user: "graduates"});
- } else if (result_m) {
+ } else if (result_m.rows.length > 0) {
return res.json({ id: result_m.rows[0].id, user: "mentors" });
} else {
return res.json("email isn't found");
}
} catch (err) {
- return res.status(500).send(err, "server error");
+ return res.status(500).send("server error");
}
Upvotes: 2