Answer the question
In order to leave comments, you need to log in
How to export model in mongoose?
Hello gentlemen.
I started to actively study mongodb + mongoose + nodejs, I ran into a problem that I can’t solve for half a day, googling doesn’t help either.
there is a modelGroups.js file :
module.exports = function(mongoose)
{
var db = mongoose.connection;
var groupSchema = new mongoose.Schema({
name: {type: String, default: 'test' },
name2: {type: String, default: 'none'}
});
// Специально, чтобы проверить, обьясню позже
groupSchema.pre('save', function(next){
console.log('saving ');
return next();
})
// Специально, чтобы проверить, обьясню позже
groupSchema.post('saved', function(next){console.log('saved');})
var model = db.model('groupModel', groupSchema);
return model;
}
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost/notes');
db.on('error', console.error.bind(console, "connection error:"));
db.once('open', function callback () { console.log('Connected to database') });
// .............................................
app.get('/test', function(req, res){
var model = require('./model/modelGroups.js')(mongoose);
var m1 = new model({name:'test g 1'});
m1.save(function(){
res.send('saved');
});
});
Answer the question
In order to leave comments, you need to log in
Googled.
For anyone interested, here:
stackoverflow.com/questions/10156623/mongoose-js-i...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question