I try to simply save a new User in my mongodb base into a controller file :
var mongoose = require('mongoose');
var UserModel = mongoose.model('User');
// signup
exports.create = function (req, res) {
console.log(req.body);
var user = new UserModel({
username: req.body.username,
password: req.body.password,
email: req.body.email
});
user.save(function (err) {
if (!err) {
return console.log("created");
}
else {
return console.log(err);
}
});
}
In debug mode, I can see that user.save() is not executed without error…
I don’t know why because I get correct value in username / password / email field.
Here my model file :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var objectId = Schema.ObjectId;
var userSchema = mongoose.Schema({
username: String,
password: String,
email: String
});
var userModel = mongoose.model('User', userSchema);
EDIT :
In my app.js I have the connection to mongodb :
mongoose.connect('mongodb://localhost/mydb', function(err){
if(err) {
console.log(err);
}
});
var userModel = require('./models/user'),
userController = require('./controllers/users');
I grouped all (controller + model) in one file to test :
And I get the same issue… don’t understand. Maybe issue in Express configuration ?
EDIT :
Issue solved with :
app.js
model.js