node.js – Trying to access express req.session.variable declared in one route from another route via post method

With my assumption that there is only single session variable per node application, i’m trying to access value set in session object in one route, from the other route

Kindly help me with this issue

app.post(/OTP) one route

 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);
    })
})

another route (/submit)

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   

This is my session object configuration

     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
     }
   }))
  • I tried alot but nothing works, when i console req.session.OTP in /OTP route it give me value of OTP but when i try to console with the /Submit route it show me undefined,
    when i try to do the same with get method it works*