以下是我的其余API端点/注册。我现在遇到的问题是终端在validateEmail之后没有停止。即使在电子邮件表单验证失败并且res.send()完成后,端点也会继续。所以我一直收到错误'错误:发送后无法设置标题。'。我希望能够在其功能中完成端点,如validateEmail,checkEmailInUse,makeUser等。
router.post("/signup", async (req, res, next) => {
const { email, password } = req.body;
const users = req.app.get("users");
validateEmail(res, email);
await checkEmailInUse(res, users, email);
const user = await makeUser(res, users, email, password);
res.send({ message: "POST signup request OK", user });
});
function validateEmail(res, email) {
const isEmail = emailFilter.test(email);
if (!isEmail) {
res.status(400).send({
error: {
message: "Requested email is not email type",
type: "FormatValidation",
location: "validateEmail"
}
});
return;
}
}
async function checkEmailInUse(res, users, email) {
const query = { email };
try {
const user = await users.findOne(query);
if (user) {
res.send({ message: "The email is already used" });
}
} catch (err) {
res.status(400).send({
error: {
message: "Failed to find user",
type: "DatabaseError",
location: "checkEmailInUse"
}
});
return;
}
}
答案 0 :(得分:0)
验证失败后代码会继续运行,因为您调用了:
validateEmail(res, email);
然后你的代码继续前进。这是Javascript中的正常控制流程。您的函数会一直执行代码行,直到您在函数中return
为止。同样的问题适用于checkEmailInUse()
。如果您希望有时在这些函数中发送响应并完成,那么您需要从可以检查的那些函数返回值,然后使用if
语句来确定您的代码是否应该执行更多操作。
按照你在验证函数中发送错误响应的方式(这不是我可能构造的东西),你可以从这些函数返回值并在请求处理程序中测试这些返回值,如下所示:
router.post("/signup", async (req, res, next) => {
const { email, password } = req.body;
const users = req.app.get("users");
if (validateEmail(res, email)) {
if (await checkEmailInUse(res, users, email)) {
const user = await makeUser(res, users, email, password);
res.send({ message: "POST signup request OK", user });
}
}
});
function validateEmail(res, email) {
const isEmail = emailFilter.test(email);
if (!isEmail) {
res.status(400).send({
error: {
message: "Requested email is not email type",
type: "FormatValidation",
location: "validateEmail"
}
});
return false;
}
return true;
}
async function checkEmailInUse(res, users, email) {
const query = { email };
try {
const user = await users.findOne(query);
if (user) {
res.send({ message: "The email is already used" });
return false;
} else {
return true;
}
} catch (err) {
res.status(400).send({
error: {
message: "Failed to find user",
type: "DatabaseError",
location: "checkEmailInUse"
}
});
return false;
}
}
}
但是,我认为你可能会发现如果你摆脱本地函数会更简单,因为当你发送一个响应时,你可以直接从主函数return
完成。以下是它的外观:
router.post("/signup", async (req, res, next) => {
function err(res, message, type, location) {
res.status(400).send({error: {message, type, location}});
}
const { email, password } = req.body;
if (!emailFilter.test(email)) {
err(res, "Requested email is not email type", "FormatValidation", "validateEmail");
return;
}
const users = req.app.get("users");
try {
const user = await users.findOne({email});
if (user) {
res.send({ message: "The email is already used" });
return;
}
} catch(e) {
err(res, "Failed to find user", "DatabaseError", "checkEmailInUse");
return;
}
try {
const user = await makeUser(res, users, email, password);
res.send({ message: "POST signup request OK", user });
} catch(e) {
err(res, "Failed to make user", "DatabaseError", "makeUser");
}
}