编译 Mongoose 后无法覆盖模型 [英] Cannot overwrite model once compiled Mongoose
问题描述
不知道我做错了什么,这是我的 check.js
Not Sure what I'm doing wrong, here is my check.js
var db = mongoose.createConnection('localhost', 'event-db');
db.on('error', console.error.bind(console, 'connection error:'));
var a1= db.once('open',function(){
var user = mongoose.model('users',{
name:String,
email:String,
password:String,
phone:Number,
_enabled:Boolean
});
user.find({},{},function (err, users) {
mongoose.connection.close();
console.log("Username supplied"+username);
//doSomethingHere })
});
这是我的 insert.js
and here is my insert.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/event-db')
var user = mongoose.model('users',{
name:String,
email:String,
password: String,
phone:Number,
_enabled:Boolean
});
var new_user = new user({
name:req.body.name,
email: req.body.email,
password: req.body.password,
phone: req.body.phone,
_enabled:false
});
new_user.save(function(err){
if(err) console.log(err);
});
每当我尝试运行 check.js 时,我都会收到此错误
Whenever I'm trying to run check.js, I'm getting this error
一旦编译就无法覆盖用户"模型.
我知道这个错误是由于架构不匹配造成的,但我看不出这是在哪里发生的?我对猫鼬和 nodeJS 还很陌生.
I understand that this error comes due to mismatching of Schema, but I cannot see where this is happening ? I'm pretty new to mongoose and nodeJS.
这是我从 MongoDB 的客户端界面得到的信息:
Here is what I'm getting from the client interface of my MongoDB:
MongoDB shell version: 2.4.6 connecting to: test
> use event-db
switched to db event-db
> db.users.find()
{ "_id" : ObjectId("52457d8718f83293205aaa95"),
"name" : "MyName",
"email" : "myemail@me.com",
"password" : "myPassword",
"phone" : 900001123,
"_enable" : true
}
>
推荐答案
出现此错误的另一个原因是,如果您在不同的文件中使用相同的模型,但您的 require
路径具有不同的大小写.
Another reason you might get this error is if you use the same model in different files but your require
path has a different case.
例如,在我的情况下,我在一个文件中有 require('./models/User')
,然后在另一个需要访问 User 模型的文件中,我有 require('./models/user')
.
For example, in my situation I had require('./models/User')
in one file, and then in another file where I needed access to the User model, I had require('./models/user')
.
我猜是查找模块 &猫鼬将其视为不同的文件.一旦我确定大小写匹配,就不再是问题了.
I guess the lookup for modules & mongoose is treating it as a different file. Once I made sure the case matched in both it was no longer an issue.
这篇关于编译 Mongoose 后无法覆盖模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!