簡體   English   中英

嘗試通過 post 方法從另一條路線訪問在一條路線中聲明的 express req.session.variable

[英]Trying to access express req.session.variable declared in one route from another route via post method

我假設每個節點應用程序只有一個 session 變量,我試圖從另一條路線訪問 session object 中設置的值

請幫我解決這個問題

app.post(/OTP) 一條路線

 function OTPgenerator(req,res,next){
    var string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    
    let OTPvalue='';
   // Find the length of string 
   var len = string.length; 
   for (let i = 0; i < 8; i++ ) { 
       OTPvalue += string[Math.floor(Math.random() * len)]; 
   } 
 console.log(OTPvalue,"OTP value");
 req.OTP=OTPvalue;
 req.session.OTP=OTPvalue;
 req.session.save();
 console.log(req.session); //this gives me value of session having OTP in it
 next();
 


  app.post('/OTP',OTPgenerator,(req,res)=>{

 const OTP={
     username:req.body.username,
     useremail:req.body.useremail,
     OTP:req.OTP
 }
    confirmation_email(OTP)
    .then((resp)=>{
        console.log("this is from the user info ",resp);
     
   
     
      res.send(req.session);

      
    })
    .catch((err)=>{
        console.log("this is the error ",err);
    })
})

另一條路線(/提交)

app.post('/submit',
(req,res)=>{
    

console.log(req.session,"session") //the session here doesn't contain value of defined before

 if(req.body.OTP!=null){
    if(req.session.OTP===req.body.OTP){

   
    const data=new userdata({
        _id:mongoose.Types.ObjectId(),
        username:req.body.username,
        userpassword:req.body.userpassword,
        userage:req.body.userage,
        useremail:req.body.useremail
    })

      
    connectiondb()
    .then(async(resp)=>{
      console.log("Result of the connection",resp);
      await data.save()
      .then(resp=>{
          console.log("this is response",resp);
          res.status(200).send("data successfully saved")
          
      })
      .catch(err=>{
          console.log("data is not saved ",err)
      })
      
      

    })
    .catch(err=>{
        console.log("thei si the error ",err);
        res.sendStatus(500);
    })

  
 }

else if(req.body.OTP===''){

    console.log("OTP cannot be null");
    res.status(422);
}
else{
console.log('OTP is invalid');
res.status(422).send(req.session);


}




 }
 else{
     res.status(500);
 }
     

    
  
})
//for signin   

這是我的 session object 配置

     const db='mongodb+srv:xxxxx';
        const obj={
         useNewUrlParser:true,
           useUnifiedTopology:true
        }

        const connection=mongoose.createConnection(db,obj);

    const sessionStore=new MongoStore({
         mongooseConnection:connection,
         collection:'usersessions'
        })

  app.use(session({
    secret: 'keyboard cat',
    resave: true,
    saveUninitialized: false,
    store: sessionStore,
     cookie:{
          secure:true
     }
   }))
  • 我嘗試了很多但沒有任何效果,當我在 /OTP 路由中控制台 req.session.OTP 時,它給了我 OTP 的值,但是當我嘗試使用 /Submit 路由控制台時,它顯示我未定義,當我嘗試對 get 執行相同操作時它的工作方法*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM