admin管理员组文章数量:1026989
I am trying to write a basic mongoose application which has a schema. I had also created a model. The code is as follows.` \
var mongoose=require('mongoose');
dbUrl='mongodb://localhost:27017/trial';
mongoose.connect(dbUrl);
var userSchema=new mongoose.Schema({
name:String,
email:String,
createdOn:Date
},{collection:'users'});
mongoose.model('User', userSchema);
var newUser=new User({name:'Simon',
email:'[email protected]',
createdOn:Date.now()
});
newUser.save(function(err){
if(!err){
console.log('User Saved');
}
});
console.log(userOne.name);
mongoose.connection.on('connected',function(){
console.log('Mongoose connected'+dbUrl);
});
mongoose.connection.on('error',function(err){
console.log('Error'+err);
});
mongoose.connection.on('disconnected',function(){
console.log('disconnected');
});
I am trying to write a basic mongoose application which has a schema. I had also created a model. The code is as follows.` \
var mongoose=require('mongoose');
dbUrl='mongodb://localhost:27017/trial';
mongoose.connect(dbUrl);
var userSchema=new mongoose.Schema({
name:String,
email:String,
createdOn:Date
},{collection:'users'});
mongoose.model('User', userSchema);
var newUser=new User({name:'Simon',
email:'[email protected]',
createdOn:Date.now()
});
newUser.save(function(err){
if(!err){
console.log('User Saved');
}
});
console.log(userOne.name);
mongoose.connection.on('connected',function(){
console.log('Mongoose connected'+dbUrl);
});
mongoose.connection.on('error',function(err){
console.log('Error'+err);
});
mongoose.connection.on('disconnected',function(){
console.log('disconnected');
});
` But when I tried to instantiate the model, I'm getting the following error.
var newUser=new User({name:'Simon',
^
ReferenceError: User is not defined
at Object.<anonymous> (C:\Users\jijebara\Desktop\IoT\Node+Mongo\m_app.js:12:
17)
at Module._pile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
I am also sure that the connections are sure since the console messages in connection.on work properly.
Any ideas on how to solve this .. Thanks in advance..
Share Improve this question edited May 28, 2023 at 19:48 George Lords of Castle 1,6954 gold badges15 silver badges33 bronze badges asked Sep 29, 2015 at 15:04 jba065jba065 3511 gold badge6 silver badges12 bronze badges2 Answers
Reset to default 4You need to import your newly created model. Just add this before using new User
var User = mongoose.model('User');
Don't use model.
const User = ("User", userSchema);
I am trying to write a basic mongoose application which has a schema. I had also created a model. The code is as follows.` \
var mongoose=require('mongoose');
dbUrl='mongodb://localhost:27017/trial';
mongoose.connect(dbUrl);
var userSchema=new mongoose.Schema({
name:String,
email:String,
createdOn:Date
},{collection:'users'});
mongoose.model('User', userSchema);
var newUser=new User({name:'Simon',
email:'[email protected]',
createdOn:Date.now()
});
newUser.save(function(err){
if(!err){
console.log('User Saved');
}
});
console.log(userOne.name);
mongoose.connection.on('connected',function(){
console.log('Mongoose connected'+dbUrl);
});
mongoose.connection.on('error',function(err){
console.log('Error'+err);
});
mongoose.connection.on('disconnected',function(){
console.log('disconnected');
});
I am trying to write a basic mongoose application which has a schema. I had also created a model. The code is as follows.` \
var mongoose=require('mongoose');
dbUrl='mongodb://localhost:27017/trial';
mongoose.connect(dbUrl);
var userSchema=new mongoose.Schema({
name:String,
email:String,
createdOn:Date
},{collection:'users'});
mongoose.model('User', userSchema);
var newUser=new User({name:'Simon',
email:'[email protected]',
createdOn:Date.now()
});
newUser.save(function(err){
if(!err){
console.log('User Saved');
}
});
console.log(userOne.name);
mongoose.connection.on('connected',function(){
console.log('Mongoose connected'+dbUrl);
});
mongoose.connection.on('error',function(err){
console.log('Error'+err);
});
mongoose.connection.on('disconnected',function(){
console.log('disconnected');
});
` But when I tried to instantiate the model, I'm getting the following error.
var newUser=new User({name:'Simon',
^
ReferenceError: User is not defined
at Object.<anonymous> (C:\Users\jijebara\Desktop\IoT\Node+Mongo\m_app.js:12:
17)
at Module._pile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
I am also sure that the connections are sure since the console messages in connection.on work properly.
Any ideas on how to solve this .. Thanks in advance..
Share Improve this question edited May 28, 2023 at 19:48 George Lords of Castle 1,6954 gold badges15 silver badges33 bronze badges asked Sep 29, 2015 at 15:04 jba065jba065 3511 gold badge6 silver badges12 bronze badges2 Answers
Reset to default 4You need to import your newly created model. Just add this before using new User
var User = mongoose.model('User');
Don't use model.
const User = ("User", userSchema);
本文标签: javascriptReferenceError 39model39 is not defined in mongooseStack Overflow
版权声明:本文标题:javascript - ReferenceError 'model' is not defined in mongoose - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745670901a2162450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论