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

Postman提示findByIdAndUpdate不是函数问题求助

问题:Postman调用接口时报错 findByIdAndUpdate 不是函数

服务器启动无语法错误,已尝试排查bug、修改模型名称、重启IDE和Postman,问题仍存在。

常见原因及解决方法

  • 模型导出错误:确保模型文件中导出的是mongoose编译后的模型,而非Schema对象
    正确的模型导出写法:

    const mongoose = require('mongoose');
    const userSchema = new mongoose.Schema({
      // 你的字段定义
    });
    
    // 必须通过mongoose.model()编译后导出
    module.exports = mongoose.model('User', userSchema);
    

    错误示例(直接导出Schema):

    // 错误:这样导出的Schema没有findByIdAndUpdate方法
    module.exports = userSchema;
    
  • 模型导入错误:检查路由/控制器中导入模型的路径和变量是否正确,确保导入的是编译后的模型实例,而非其他对象

    // 正确导入模型
    const User = require('../models/User');
    
  • 调用对象错误:确认调用findByIdAndUpdate的是模型本身(该方法为模型静态方法),而非Schema或其他自定义对象

内容的提问来源于stack exchange,提问作者Tanishq Rinjay

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:02:36