如果获取请求失败,如何停止功能
how to stop function if a fetch request fails
我正在使用 Google 验证码 v3 构建电子邮件表单。
我希望如果分数小于 1,则函数(请求)的其余部分应该结束。
但问题是,如果我将 return
语句添加到我的获取请求中,它就会退出 .then()
函数并且不会停止请求。
代码如下:
app.post(
"/mail",
(req, res) => {
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.SECRET_KEY}&response=${req.body.token}`;
fetch(url, {
method: "post",
})
.then((response) => response.json())
.then((google_response) => {
console.log(google_response);
if ((google_response.success = false || google_response.score < 1)) {
console.log("ROBOT ALERT");
res.status(422).json({
captcha: "Robot verification failed. Try again later",
});
return; //------------here I want to stop the continuation of the request----
}
return;
})
.catch((error) => {
console.log(error);
res.json({ captcha: "An unknown error occurred. Try again later" });
});
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors);
return res.status(422).json({ errors: errors.array() });
}
//If everything is ok then end request here.
res.json({ success: true });
}
})
在 then 里面做所有的事情:
fetch(url, options).then(response => response.json().then(data => {
// do everything here
})).catch(e => {
// handle error
})
你可以在if语句中使用return来在if语句被触发后停止执行函数:
app.post("/mail", (req, res) => {
fetch(url, options)
.then((response) => response.json())
.then((googleResponse) => {
if ((google_response.success = false || google_response.score < 1)) {
console.log("ROBOT ALERT");
return res.status(404).send({ captcha: "Robot verification failed. Try again later" });
// Should stop here if condition is triggered
}
// Continue here if the condition is not triggered
});
});
我正在使用 Google 验证码 v3 构建电子邮件表单。
我希望如果分数小于 1,则函数(请求)的其余部分应该结束。
但问题是,如果我将 return
语句添加到我的获取请求中,它就会退出 .then()
函数并且不会停止请求。
代码如下:
app.post(
"/mail",
(req, res) => {
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${process.env.SECRET_KEY}&response=${req.body.token}`;
fetch(url, {
method: "post",
})
.then((response) => response.json())
.then((google_response) => {
console.log(google_response);
if ((google_response.success = false || google_response.score < 1)) {
console.log("ROBOT ALERT");
res.status(422).json({
captcha: "Robot verification failed. Try again later",
});
return; //------------here I want to stop the continuation of the request----
}
return;
})
.catch((error) => {
console.log(error);
res.json({ captcha: "An unknown error occurred. Try again later" });
});
// Finds the validation errors in this request and wraps them in an object with handy functions
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors);
return res.status(422).json({ errors: errors.array() });
}
//If everything is ok then end request here.
res.json({ success: true });
}
})
在 then 里面做所有的事情:
fetch(url, options).then(response => response.json().then(data => {
// do everything here
})).catch(e => {
// handle error
})
你可以在if语句中使用return来在if语句被触发后停止执行函数:
app.post("/mail", (req, res) => {
fetch(url, options)
.then((response) => response.json())
.then((googleResponse) => {
if ((google_response.success = false || google_response.score < 1)) {
console.log("ROBOT ALERT");
return res.status(404).send({ captcha: "Robot verification failed. Try again later" });
// Should stop here if condition is triggered
}
// Continue here if the condition is not triggered
});
});