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

Mongoose TypeError:Views.updateOne不是函数的问题求助

解决Mongoose中updateOne is not a function错误

问题根源

你遇到的错误核心原因是:你导入的Views不是Mongoose模型,而是Schema对象。Schema本身没有updateOne这类静态操作方法,只有通过mongoose.model()创建的模型才具备完整的CRUD操作能力。

修复步骤

  1. 检查并修正../models/views.js的导出代码,确保导出的是Mongoose模型而非Schema:
const mongoose = require('mongoose');

// 定义你的viewSchema
const viewSchema = new mongoose.Schema({
  numberOfViews: { type: Number, default: 0 },
  createdAt: { type: Date, default: Date.now }
});

// 关键:导出模型,不是Schema
module.exports = mongoose.model('Views', viewSchema);
  1. 确认导入路径无误,保证require('../models/views')指向的是上述正确导出模型的文件。

验证效果

修正后,Views将成为具备完整CRUD方法的Mongoose模型,你的updateViews函数可以正常执行:

  • 匹配当日的文档并自增numberOfViews字段
  • 若当日对应文档不存在,通过upsert: true自动创建新文档

内容的提问来源于stack exchange,提问作者aymane RIhane

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 22:29:51