node.js - I don't understand why I am getting a "no overload match for this call" error - Stack Overfl

admin2025-04-30  0

I have generated code, which is for registering and logging in users, I am getting an overload issue on the Login, but not the register. I have added returns to ever location in that function that i could find, but it does not resolve the issue. My issue is not during runtime, but just trying to start up the server.

This is my users.ts file

   import express, { Request, Response, NextFunction } from 'express';
import { Pool } from 'pg';
import bcrypt from 'bcrypt';
import generateJWT from '../jwtUtils.ts'; // Import the generateJWT function

const router = express.Router();

const pool = new Pool({
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  password: process.env.DB_PASSWORD,
  port: Number(process.env.DB_PORT),
});

// Register a new user
router.post('/register', async (req: Request, res: Response, next: NextFunction) => {
  try {
    const { name, email, password } = req.body;
    const hashedPassword = await bcrypt.hash(password, 10); 

    const result = await pool.query(
      'INSERT INTO users (name, email, password) VALUES ($1, $2, $3) RETURNING *',
      [name, email, hashedPassword]
    );

    res.status(201).json(result.rows[0]); 
  } catch (error) {
    console.error("Error registering user:", error);
    res.status(500).json({ error: 'Failed to register user' });
  }
});

// Login a user
router.post('/login', async (req: Request, res: Response) => {  // this line shows the no overload matches to this call
  try {
    const { email, password } = req.body;

    const result = await pool.query('SELECT * FROM users WHERE email = $1', [email]);

    if (result.rows.length === 0) {
      return res.status(401).json({ error: 'Invalid credentials' }); //-- this line causes the error above
    }

    const user = result.rows[0];
    const passwordMatch = await bcryptpare(password, user.password);

    if (!passwordMatch) {
      res.status(401).json({ error: 'Invalid credentials' });
    }

    const token = generateJWT(user); 

    res.json({ user, token }); 
  } catch (error) {
    console.error("Error logging in user:", error);
    res.status(500).json({ error: 'Login failed' });
  }
});

export default router;

the error I have is:

No overload matches this call.
  The last overload gave the following error.
    Argument of type '(req: Request, res: Response, next: NextFunction) => Promise<express.Response<any, Record<string, any>>>' is not assignable to parameter of type 'Application<Record<string, any>>'.
      Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<...>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769)
index.d.ts(164, 5): The last overload is declared here.

I have removed all the returns, but if I could get the server started, I am not sure it will give me what I need yet.

SOLUTION**** for me, the solution is to remove the "return" from the code, i can only assume the return is handled by the res.?? function. I cannot provide this as an answer, but will leave it here in case anyone else comes looking.

转载请注明原文地址:http://anycun.com/QandA/1746028168a91549.html