0

I am using session to store user data coming from sign-upform and if user fill anything wrong and submit it then I am redirecting it to back to sign-uppage with all the form data he has fill. Now on the post route when someone submit data, I check all the field condition and if there is some error then I make an object "req.session.inputData(inputData is my var name)" and store the user submit data there. Now on the get route, I checked "req.session.inputData" and if nothing is there then is just null all the fields on the object and uses ejs(template enjine) to inject into my front end.

const express = require("express");
const db = require('../service/mongo');


async function httpGetSignUp(req, res) { // get route sign-up page          
let sessioninputData = req.session.inputData; //checking if session has some inputData    
if (!sessioninputData) {      //if no session data then create an empty one              
sessioninputData = {
  hasError: false,
  name: "",
  userName: "",
  email: "",
 };
}
req.session.inputData = null;        
res.render("sign-up", { inputData: sessioninputData });//inject session data to frontend        
}

  
 async function httpPostSignUp(req,res){ // post route sign-up page
  const userData = req.body;
  const enteredName = userData.name;
  const enteredUserName = userData.userName;
  const enteredEmail = userData.email;
  const enteredPassword = userData.password;
  const enteredConfirmPassword = userData.confirmPassword;

  // check for validation on form field
  if (                                                
  !enteredEmail ||
  !enteredConfirmPassword ||
  !enteredPassword ||
  enteredPassword.trim().length < 6 ||
  enteredPassword !== enteredConfirmPassword ||
  !enteredEmail.includes("@")
   ) {      
   req.session.inputData = {                 // if found error then create an object    
    hasError: true,
    message: "invalid input- please try again",
    name:enteredName,
    userName: enteredUserName,
    email: enteredEmail,
   };       
   req.session.save(function () {            //save the session and redirect to signup   
    console.log("Incorrect data");
    res.redirect("/sign-up");
     });
   return;
   }
  
  // check database for existing email
  const existingUser = await existingUser(enteredEmail);       
  if (existingUser) {        // if existing email found again create an session object    
  req.session.inputData = {
    hasError: true,
    message: "Email already in use",
    name: enteredName,
    userName: enteredUserName,
    email: enteredEmail,
    };       
    req.session.save(function () {    //save session and redirect to sign-up page
     console.log("Email already in use");
    return res.redirect("sign-up");
    });

  //check database for existing username
  const existingUserName = await existingUserName(enteredUserName);      
  if (existingUserUserName) {  // if existing username found, create an session object          
   req.session.inputData = {
    hasError: true,
     message: "User Name already in use",
    name: enteredName,
    userName: enteredUserName,
    email: enteredEmail,
    };      
   req.session.save(function () { //save session data and redirect it to sign-up page
     console.log("User Name already in use");
     return res.redirect("sign-up");
   });
   }


  // if no issue occur the hash password and save to dbs
  const hashPassword = await bcrypt.hash(enteredPassword, 12);
  const user = {
  name: enteredName,
  userName: enteredUserName,
  email: enteredEmail,
  password: hashPassword,
  };

 // save to dbs
 const response = await db.getDb().collection("users").insertOne(user);

 //redirect to sign-in page
 return res.redirect("/sign-in");
 }

      //error I am getting
      let sessioninputData = req.session.inputData;
                                         ^
      TypeError: Cannot read properties of undefined (reading 'inputData')
  • are you using `express-session` package? can you show us how you set express up? i.e. `app.use`, `app.get`? – about14sheep Apr 22 '22 at 18:42
  • Does this answer your question? [req.session is undefined using express-session](https://stackoverflow.com/questions/39796228/req-session-is-undefined-using-express-session) – about14sheep Apr 22 '22 at 18:44

0 Answers0