unable to access property saved in request object of express

I am a beginner to Typescript. while i was trying to save a property in req session object of express after decleration i was expexting it to reproduce the property in other function which is called by different API call but getting undefined.

i tried every possible solution but ubale to get solution.
that’s how my express.d.ts file looks.

  declare module 'express-session' {
  export  interface SessionData {
      user: {
         email: string,
         name: string,
         profileImage: string,
         mobileNumber: string,
         status: string
   };
    }
  }
        

In below function i was trying to put user object in request

const getUserData = async (req: Request, res: Response) => {
    const email=req.body.email;
    const partial= req.body.partial;
    let response: user| null =null;
    
    try{
        response= await prisma.user.findUnique({
            where:{
                email:email
            }
        })
        console.log(req.user,req.session.user,'session');
        req.session.user = {
            email: response!.email,
            name: response!.name,
            profileImage: response!.profileImage ?? '',
            mobileNumber: response!.mobileNumber ?? '',
            status: response!.status ?? ''
        };

        console.log(req.user)
        const session= req.session.save((err) => {
            console.log("session saved");
            if (err) {
                console.log(err);
            }

        })
                
    }
    catch(error){
        return res.json({message:'User not found'});
        console.log(error);
    }
    console.log(response);
    if(partial){
        return res.status(200).json({name:response?.name, email:response?.email, profileImage:response?.profileImage, status:response?.status});
    }
    return res.status(200).json(response);
}

and i try to access user in

const sendRequest = async (req: Request, res: Response) => {
    const email: any  = req.session.user?.email;
    console.log(email);
    console.log(req.session);
    
    const friendEmail = req.body.email;
    let response: user | null = null;
    let responseFriend: user | null = null;
    try {
        if(email!==undefined){
        response = await prisma.user.update({
            where: {
                email: email
            },
            data: {
                userRequesting: {
                    push: friendEmail
                }
            }
        })
    }
        responseFriend = await prisma.user.update({
            where: {
                email: friendEmail
            },
            data: {
                userRequested: {
                    push: email
                }
            }
    })
        res.status(200).json({ message: 'Request sent successfully' });

    } catch (error) {
        console.log(error);
    }
}

this is how tsconfig.json file looks

{
  "compilerOptions": {

    "target": "es2016",                                  /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */


    /* Modules */
    "module": "commonjs",                                /* Specify what module code is generated. */
    "rootDir": "./src",                                  /* Specify the root folder within your source files. */

    "typeRoots": [ "./node_modules/*", "./src/*" ],                                  /* Specify multiple folders that act like './node_modules/@types'. */

 
    "sourceMap": true,                                /* Create source map files for emitted JavaScript files. */   // "outFile": "./",                                  /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
    "outDir": "./dist",                                   /* Specify an output folder for all emitted files. */

    "esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
    "forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */

    "strict": true,                                      /* Enable all strict type-checking options. */
    "skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
  },
  "include": ["src/**/*.ts", "src/*.d.ts"],
}

for session storage i am using redis
and this is my index.ts file

import express from 'express';
import cors from 'cors';
import session from 'express-session';
import RedisStore from 'connect-redis';
import client from './model/client';
import multer from 'multer';
import { userRouter } from './routes/user';
import { postRouter } from './routes/posts';
import path from 'path';
import { Request,Response } from 'express';
import { NextFunction } from 'connect';
const app = express();

// Session Configuration
const redisstoreInstance = new RedisStore({ client: client });
const sessionMiddleware= session({
    secret: 'my secret',
    resave: false,
    saveUninitialized: true,
    store: redisstoreInstance
  })
  app.use(
    sessionMiddleware,
  );

// Middleware Setup
app.use(cors({
    origin: 'http://localhost:5173',
    credentials: true // Important to allow cookies to be sent
})); 
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
 // Ensure this is before the routes

app.use('/uploads', express.static(path.join(__dirname, '../uploads')));
app.use(multer({
    storage: multer.diskStorage({
        destination: (req, file, cb) => {
            cb(null, 'uploads/');
        },
        filename: (req, file, cb) => {
            cb(null, file.originalname);
        }
    }),
    fileFilter: (req, file, cb) => {
        if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
            cb(null, true);
        } else {
            cb(null, false);
        }
    }
}).single('profileImage'));
app.use((req: Request, res: Response, next: NextFunction) => {
    req.user = req.session.user as { email: string; name: string; profileImage: string; mobileNumber: string; status: string; };
    next()
});
// Routers
app.use('/api/v1/user', userRouter);
app.use('/api/v1/posts', postRouter);

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

please help me with the issue. it looks like i am missing something.

New contributor

saharsh vashishtha is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật