运行Node.js脚本报错‘Schema has not been registered for model’求助
Hey there, let's break down why you're hitting that frustrating "Schema has not been registered for model" error. Looking at your code snippets, here are the key issues to fix:
1. 模型文件缺少关键的模型注册步骤
Your model file cuts off after defining the Schema, but the critical step of registering that Schema as a Mongoose Model is missing. Mongoose needs you to explicitly create a model from your schema so it can recognize it later when you try to use it.
Here's what your complete csvUpload.js model file should look like (fill in your actual schema fields):
var mongoose = require('mongoose'); var Schema = mongoose.Schema; // 补全你的Schema字段定义(示例) var csvUploadsSchema = new Schema({ filename: { type: String, required: true }, uploadTimestamp: { type: Date, default: Date.now }, fileContent: String }); // 核心步骤:注册模型,第一个参数是模型名称(后续调用必须完全匹配) mongoose.model('CsvUpload', csvUploadsSchema);
When you want to use this model elsewhere in your app, reference it using the exact name you registered:
var CsvUpload = mongoose.model('CsvUpload');
2. Mongoose连接存在拼写错误
In your app.js, you have a typo in the connect call: mongoos.connect is missing an 'e'—it should be mongoose.connect. This typo would prevent your app from connecting to MongoDB properly, which can also indirectly trigger schema/model registration errors.
Fixed line in app.js:
mongoose.connect('mongodb://localhost/csvUploads', { useMongoClient: true });
额外提醒
- Model names are case-sensitive! Make sure
CsvUpload(or whatever name you choose) is spelled exactly the same everywhere you reference it. - If you're using Mongoose 4.11 or newer, the
useMongoClientoption is deprecated—you can safely remove it since the new driver is used by default.
内容的提问来源于stack exchange,提问作者Reinard Carranceja




