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 8410365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:09:15+00:00 2026-06-10T00:09:15+00:00

I am new to Mongoose, and I am trying to do a simple test

  • 0

I am new to Mongoose, and I am trying to do a simple test app that involves using Mongoose in order to familiarize myself with it. I can load up the “/” page fine, but when I submit the form, it stays on the “/” page until I terminate the process. The console.logs “before” and “after” print, but the during never does. What is wrong?

var express = require('express');
var mongoose = require('mongoose')
  , db = mongoose.createConnection('localhost', 'testmongoose');

var connectdb = function(execute) {
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function () {
        execute();
    });
};

connectdb(function () {
    var postSchema = new mongoose.Schema({
        body: String
    });

    var Post = db.model('Post', postSchema);
});

var app = express();

app.configure(function () {
    //app.use(express.logger());
    app.use(express.bodyParser());
    app.use(express.static(__dirname + '/static'));
});

app.set('views', __dirname + '/views');
app.set('view engine','jade');

app.get('/', function(request, response) {
    response.render('index');
});

app.post('/result', function(request, response) {
    var text = request.body.text;
    console.log("before!");
    connectdb(function () {
        console.log("during!");
        var post = "test";
        post = new Post({body: text});
        response.render('result', {text: post.body});
    });
    console.log("after!");
});

app.listen(4000);
  • 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-10T00:09:16+00:00Added an answer on June 10, 2026 at 12:09 am

    First of all, since connectdb is an asynchronous function, it returns immediately and runs its callback later–meaning that the order of your console.logs will be “before!” then “after!” and then “during!” sometime later.

    That said, connectdb isn’t really serving you any use. Mongoose automatically buffers up commands until it connects, so you can treat it like a synchronous interface:

    var express = require('express');
    var mongoose = require('mongoose');
    
    mongoose.connect('mongodb://localhost', 'testmongoose');
    mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
    
    var postSchema = new mongoose.Schema({
        body: String
    });
    
    var Post = mongoose.model('Post', postSchema);
    
    var app = express();
    
    app.configure(function () {
        //app.use(express.logger());
        app.use(express.bodyParser());
        app.use(express.static(__dirname + '/static'));
    });
    
    app.set('views', __dirname + '/views');
    app.set('view engine','jade');
    
    app.get('/', function(request, response) {
        response.render('index');
    });
    
    app.post('/result', function(request, response) {
        var text = request.body.text;
        var post = new Post({body: text});
        response.render('result', {text: post.body});
    });
    
    app.listen(4000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

new to c#. I'm trying to make a simple system where I can search
I'm trying to save myself a lot of queries, I'm using Mongoose as ODM.
I am trying to define a simple Mongoose model for a learning ExpressJS app.
I am trying to create a simple MongooseJS example program that gets a list
I am new at Mongoose/nodejs and I am struggling with a simple update of
I'm using mongoose (on node) and I'm trying to add some additional fields to
I'm trying the Develop a RESTful API Using Node.js With Express and Mongoose example
I'm trying to learn node and mongo in order to build a simple web
I am new to Node, and I'm using Mongoose as a driver for MongoDB.
I'm trying to break down my RDBMS mentality with Mongoose in a node.js app,

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.