You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express登录接口启动时出现No overload matches this call错误求助

Express登录接口"No overload matches this call"错误排查与解决

我基于Express开发了用户注册和登录接口,注册接口运行正常,但登录接口在启动服务器时抛出**"No overload matches this call"**错误。我尝试在函数的各个位置添加return语句,问题并未解决;移除return语句后服务器能正常启动,但不确定这样是否会影响功能。最终验证发现移除return是可行的解决方案。

相关代码

import express, { Request, Response, NextFunction } from 'express';
import { Pool } from 'pg';
import bcrypt from 'bcrypt';
import generateJWT from '../jwtUtils.ts'; // 导入生成JWT的工具函数

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

// 用户注册接口
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);
    res.status(500).json({ error: '注册失败' });
  }
});

// 用户登录接口 - 此处报错"No overload matches this call"
router.post('/login', async (req: Request, res: Response) => {  
  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: '无效凭据' }); // 该行触发上述错误
    }

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

    if (!passwordMatch) {
      res.status(401).json({ error: '无效凭据' });
    }

    const token = generateJWT(user); 

    res.json({ user, token }); 
  } catch (error) {
    console.error("用户登录出错:", error);
    res.status(500).json({ error: '登录失败' });
  }
});

export default router;

错误信息

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.

问题分析与解决

错误原因

TypeScript类型检查认为异步路由处理函数返回的Promise<Response>与Express路由期望的签名不匹配。当在if (result.rows.length === 0)分支中使用return res.status(401).json(...)时,函数返回类型变为Promise<Response | void>,与Express路由处理函数要求的返回值类型冲突,触发重载错误。

修正方案

移除return res.status(...)中的return,改为单独使用return终止函数执行(避免后续代码运行),同时补全密码不匹配分支的终止逻辑:

router.post('/login', async (req: Request, res: Response) => {  
  try {
    const { email, password } = req.body;

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

    if (result.rows.length === 0) {
      res.status(401).json({ error: '无效凭据' });
      return; // 单独用return终止函数,不触发类型错误
    }

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

    if (!passwordMatch) {
      res.status(401).json({ error: '无效凭据' });
      return; // 补全return,防止后续代码执行
    }

    const token = generateJWT(user); 

    res.json({ user, token }); 
  } catch (error) {
    console.error("用户登录出错:", error);
    res.status(500).json({ error: '登录失败' });
  }
});

功能验证

修正后服务器可正常启动,接口逻辑完整:

  • 用户不存在时返回401错误并终止函数
  • 密码不匹配时返回401错误并终止函数
  • 验证通过时返回用户信息与JWT令牌

内容的提问来源于stack exchange,提问作者Roger i

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 03:02:34