即使我将所有参数传递到其中,重定向路径也无法正常工作
我有一个网站的后订单部分,除了重定向路径函数外,一切都很好。我无法说明为什么此功能不起作用。当我记录RedirectPath时,它会带有我以前想要的正确路径,但不会将视图重定向到正确的路径。我还设置了重定向路径,因此这不是问题。
app.post('/postOrder/:id', async function(req, res, next){
var customerInfo = req.body.info;
var packageInfo = req.body.items.items;
var storeID = req.params['id']
let ts = Date.now();
let date_ob = new Date(ts);
let date = date_ob.getDate();
let month = date_ob.getMonth() + 1;
let year = date_ob.getFullYear();
let yourOrder = []
packageInfo.forEach((package) => cusOrder(package.product, package.qty));
function cusOrder(product, qty){
let order = {product: product, qty: qty}
yourOrder.push(order);
}
console.log(yourOrder);
const order = {
package: yourOrder,
storenum: storeID,
date: year+"/"+month+"/"+date,
first: customerInfo[0].fname,
last: customerInfo[1].lname,
address: customerInfo[2].address,
city: customerInfo[3].city,
state: customerInfo[4].state,
zipcode: customerInfo[5].zip
}
new Orders(order).save().then(home => {
res.locals.redirect = "/";
res.locals.home = home;
next()
})
.catch(error => {
console.log(`Error updating user by ID: ${error.message}`);
next(error);
});
},
function(req, res, next) {
let redirectPath = res.locals.redirect;
console.log(redirectPath);
if (redirectPath !== undefined) res.redirect(redirectPath);
else next();
}
);
I have a post order section of my website and everything is working fine except for the redirect path function. I cannot tell why this function is not working. When I log redirectPath it comes out with the correct path I want from the declaration before but will not redirect the view to the correct path. I also have the redirect path set up so that is not the problem.
app.post('/postOrder/:id', async function(req, res, next){
var customerInfo = req.body.info;
var packageInfo = req.body.items.items;
var storeID = req.params['id']
let ts = Date.now();
let date_ob = new Date(ts);
let date = date_ob.getDate();
let month = date_ob.getMonth() + 1;
let year = date_ob.getFullYear();
let yourOrder = []
packageInfo.forEach((package) => cusOrder(package.product, package.qty));
function cusOrder(product, qty){
let order = {product: product, qty: qty}
yourOrder.push(order);
}
console.log(yourOrder);
const order = {
package: yourOrder,
storenum: storeID,
date: year+"/"+month+"/"+date,
first: customerInfo[0].fname,
last: customerInfo[1].lname,
address: customerInfo[2].address,
city: customerInfo[3].city,
state: customerInfo[4].state,
zipcode: customerInfo[5].zip
}
new Orders(order).save().then(home => {
res.locals.redirect = "/";
res.locals.home = home;
next()
})
.catch(error => {
console.log(`Error updating user by ID: ${error.message}`);
next(error);
});
},
function(req, res, next) {
let redirectPath = res.locals.redirect;
console.log(redirectPath);
if (redirectPath !== undefined) res.redirect(redirectPath);
else next();
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要返回由保存功能开始的承诺,或者坚持使用异步/等待直接承诺。我个人建议使用
Ex
You need to either return the promise started by the save function or stick to using async/await instead of the promise directly. I would personally recommend using await
Ex