【问题标题】:How to pass Data Between multiple routes in express?如何在快递中的多条路线之间传递数据?
【发布时间】:2021-07-28 15:26:58
【问题描述】:
 app.post("/login", passport.authenticate("local",), function (req, res) {
  const user = new Model({
    username: req.body.username,
    password: req.body.password,
  });

  req.login(user, function (err) {
    if (err) {
      console.log("wrong password");
    } else {
      passport.authenticate("local")(req, res, function () {
        res.redirect("/admin");
      });
    }
  });
});

app.post("/admin", function (req, res) {
 
  Model.findOne({username: "siddharth"}).exec(function(err, foundList){
  if(foundList)
  {
    const list = new linkModel({
      linkTitle: req.body.linkTitle,
      linkUrl: req.body.linkUrl,
     
    });
   
    foundList.links.push(list);
    foundList.save();
   
  
   res.redirect("/admin");
  }
  else
    {res.send("redirect to home page and then login");
    res.redirect("/login");

    }
  

  });
});

当从登录路由到其他路由(管理员)进行身份验证时,我如何将用户名传递给定义了 mongoose 查询的 findone? 正如我已经明确定义的那样。

或者简单来说,我如何在路由之间传递数据?

【问题讨论】:

    标签: node.js mongodb express session mongoose


    【解决方案1】:

    你不能。而是使用中间件进行您想要的检查并将结果传递给另一个中间件或在出现错误时返回响应。

    【讨论】:

    • 我只想在他登录时将数据推送到用户集合中。如您所见,我指定了我的姓名,而不是在用户登录时获取用户名。你能详细说明一下中间件的事情吗
    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    相关资源
    最近更新 更多