即使我将所有参数传递到其中,重定向路径也无法正常工作

发布于 2025-01-28 00:29:50 字数 1477 浏览 5 评论 0原文

我有一个网站的后订单部分,除了重定向路径函数外,一切都很好。我无法说明为什么此功能不起作用。当我记录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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骷髅 2025-02-04 00:29:50

您需要返回由保存功能开始的承诺,或者坚持使用异步/等待直接承诺。我个人建议使用

Ex

const home = await new Orders(order).save()

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

const home = await new Orders(order).save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文