Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:57:29+00:00 2026-06-17T08:57:29+00:00

I try to simply save a new User in my mongodb base into a

  • 0

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');
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:57:30+00:00Added an answer on June 17, 2026 at 8:57 am

    I grouped all (controller + model) in one file to test :

    var express = require('express')
      , routes  = require('./routes')
      , userRoute    = require('./routes/user')
      , http    = require('http')
      , path    = require('path')
      , io      = require('socket.io')
      , mongoose = require('mongoose');
    
    var app = express();
    
    app.configure(function(){
      app.set('port', process.env.PORT || 8080);
      app.set('views', __dirname + '/views');
      app.set('view engine', 'ejs');
      app.use(express.favicon());
      app.use(express.logger('dev'));
      app.use(express.bodyParser());
      app.use(express.methodOverride());
      app.use(app.router);
      app.use(require('stylus').middleware(__dirname + '/public'));
      app.use(express.static(path.join(__dirname, 'public')));
    });
    
    app.configure('development', function(){
      app.use(express.errorHandler());
    });
    
    mongoose.connect('mongodb://127.0.0.1/mydb', function(err){
        if(err) {
            console.log(err);
        }
        else {
            console.log("Connected to mongo DB")
        }
    });
    
    /*var userModel = require('./models/user'),
        tchatMessageModel = require('./models/tchatMessage'),
        userController = require('./controllers/users'),
        tchatMessageController = require('./controllers/tchatMessages');       */
    
    app.get('/', routes.index);
    app.get('/signup', userRoute.signup);
    app.get('/signin', userRoute.signin);
    //app.post('/signup', userController.create);
    //app.get('/users', userRoute.list);
    
    var Schema = mongoose.Schema;
    var userSchema = Schema({
        username: String,
        password: String,
        email: String
    });
    var UserModel = mongoose.model('User', userSchema);
    app.post('/signup', 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);
            }
        });
    
    });
    
    var server = http.createServer(app);
    
    server.listen(app.get('port'), function(){
        console.log("Express server listening on port " + app.get('port'));
    });
    

    And I get the same issue… don’t understand. Maybe issue in Express configuration ?

    EDIT :

    Issue solved with :

    app.js

    mongoose.model("User", require("./models/user").userModel);
    

    model.js

    exports.userModel = userSchema;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to make a simple message controller with new/create actions, when I call
I want to save logs of user's operation by accessing log script file(for example,
I simply try to assign a background color to each div. it looks simple
I try to work with ctags and it simply doesn't work. I follow this
I'm going to try and explain this as simply as I can, it's most
I'm trying to set up a simple login using AuthLogic into my User table.
Just tried writing a simple validates_presence_of in my model, and when the errors try
I have a simple model Class class Talk(models.Model): url = models.URLField() user = models.ForeignKey(User)
What im trying to do is simply letting the user choose a directory to
I am currently attempting to have the browser automatically prompt the user to save

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.