node.js Mongoose 和新架构:返回“ReferenceError:架构未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24908405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongoose and new Schema: returns "ReferenceError: Schema is not defined"
提问by Lars Holdgaard
I am creating a new sample application, where I try to connect to a MongoDB database through Mongoose.
我正在创建一个新的示例应用程序,我尝试通过 Mongoose 连接到 MongoDB 数据库。
I create a new schema in my service.jsfile, but I get the following error when I run nodemon app.js: "ReferenceError: Schema is not defined"
我在我的service.js文件中创建了一个新架构,但运行时出现以下错误nodemon app.js:"ReferenceError: Schema is not defined"
App.js code:
App.js 代码:
var http = require('http');
var express = require('express');
var serials = require('./service');
var app = express();
var mongoose = require('mongoose');
var port = 4000;
app.listen(port);
mongoose.connect('mongodb://localhost:27017/serialnumbers')
app.get('/api/serials',function(req,res){
serials.getSerial(req, res, function(err, data) {
res.send(data);
});
});
Service.js code:
Service.js 代码:
var mongoose = require('mongoose');
var serialSchema = new Schema({
serial: {type: String},
game: {type: String},
date: {type: Date, default: Date.now},
});
mongoose.model('serials', serialSchema);
exports.getSerial = function(req,res,cb) {
mongoose.model('serials').find(function(err,data) {
cb(err,data);
});
};
I saw an answer here on StackOverflow that referenced it could be the version of Mongoose. But npm listgives me this:
我在 StackOverflow 上看到一个答案,提到它可能是 Mongoose 的版本。但npm list给我这个:


Any idea what I am doing wrong?
知道我做错了什么吗?
回答by anvarik
Exactly, in your Service.js, what is Schema? You don't have an object named Schema.
确切地说,在您的 中Service.js,什么是Schema?您没有名为 的对象Schema。
...
var serialSchema = new Schema({
^^^^^^
change it to mongoose.Schemathen it will be fine.
改成这样mongoose.Schema就好了。
回答by Tere Villalba
you forgot to define the Schema like this on you second line
你忘了在你的第二行定义这样的架构
var Schema = mongoose.Schema
var Schema = mongoose.Schema
回答by Aravinda Meewalaarachchi
This can be occurred due to several reasons. first one is that you might forgotten to import Schema.You can fix that as follows.
这可能由于多种原因而发生。第一个是您可能忘记导入架构。您可以按如下方式修复。
const Schema = mongoose.Schema;
const serialSchema = new Schema({
serial: {type: String},
game: {type: String},
date: {type: Date, default: Date.now},
});
Sometimes you have forgotten to import your newly created model.This type of errors can be solve by importing created model to your working file.
有时您忘记导入新创建的模型。这种类型的错误可以通过将创建的模型导入您的工作文件来解决。
const serialModel = mongoose.model('serials', serialSchema);
回答by Hasan Zahran
In you case call as mongoose.Schemaas you did not defined it as
在你的情况下调用,mongoose.Schema因为你没有将它定义为
const mongoose = require('mongoose');
then
const Schema = mongoose.Schema;
回答by Greg Alpha
In my case, I was referencing a schema inside of another schema and had to replace type: Schema.Types.ObjectId with mongoose.Types.ObjectId.
就我而言,我引用了另一个架构中的架构,并且不得不将 type: Schema.Types.ObjectId 替换为 mongoose.Types.ObjectId。

