javascript - 重定向到用户
问题描述
这是一个开放的代码,因为通过电子邮件结束给用户的电子邮件,当他们打开链接时,它会引导到端点处的用户。
在函数结束时,我想将用户重定向到新页面,但重定向不起作用,为什么它不起作用?
async getToken(req, res) {
// const { token } = req.params;
let { access_token } = req.query;
access_token = access_token.replace(/\s/g, '');
let decoded;
try {
decoded = jwt.verify(access_token, process.env.JWT_ACCOUNT_ACTIVATION);
const email_consulted = decoded.email; // you are returning a success response but you havent finished the process yet. Usually the success is sent on the end of the process
let select = await pool.query(
`SELECT User_email FROM user WHERE User_email='${email_consulted}'`
);
// console.log(select);
if (select.length > 0) {
return res.json({
success: false,
code: 400,
message: "Email ya existe"
});
}
const result = await pool.query(
`INSERT INTO user (User_email) VALUES ('${email_consulted}')`
);
let selectid = await pool.query(
`SELECT ID_user FROM user WHERE User_email='${email_consulted}'`
);
const id = selectid[0].ID_user
const token = jwt.sign(
{ id },
process.env.JWT_ACCOUNT_ACTIVATION,
{expiresIn: '30d' }
)
res.cookie("_$",token);
res.json({
sucess: true,
code: 201,
message: "Usuario añadido exitosamente",
token
});
return window.location.replace("http://seth.com/dashboard.html?ftime=0");
// if (select != "") {
// const result = await pool.query(`INSERT INTO user (User_email) VALUES (${email_consulted})`);
// }
} catch (err) {
return res.json({ message: "Internal server error" });
}
}
解决方案
您正在尝试在服务器上重定向,而不是在客户端上。 window
未在服务器上定义,因此这将导致错误。使用该res
对象返回您的响应,就像您已经在其他地方所做的那样。在这种情况下,响应是重定向:
res.redirect('http://seth.com/dashboard.html?ftime=0');
旁注:您不需要,只需适当地return res...
调用函数即可。res
对象本身(以及随后的res
框架)负责响应客户端,而不是您。
推荐阅读
- embedded - 在 C 代码中使用带有 MSP432 微控制器的 I2C 16x2 LCD
- macos - 在 shell 脚本中指定库的“配置”脚本特定路径时,如何使用相对路径?
- php - 如何在仅创建 S3 对象预签名 URL 时解决内存耗尽问题
- apache-spark - 胶水如何使用pyspark删除数据帧上的记录
- javascript - ES6 箭头函数 () 与 _
- git - 为什么 PyCharm 不验证 Git 提交
- android - 在 Kotlin 中创建 SAM 接口的实例时,为什么需要在接口名称后加上括号?
- python - Django 表单中的 HTML 必需属性
- python - 如何在python中求解雅可比椭圆函数
- python - 如何模拟随机过程,直到路径的所有元素都是正数?