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

  • Home
  • SEARCH
  • 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 7069595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:27:52+00:00 2026-05-28T05:27:52+00:00

How do I get an asynchronous result back in nodeunit and mongoose? I’ve tried

  • 0

How do I get an asynchronous result back in nodeunit and mongoose? I’ve tried the following code and it seems to hang on the database callback never returning a result or err.

mongoose = require "mongoose"
models = require "../Services/models"
Task = models.Task

module.exports =
setUp: (callback) ->
    try
        @db = mongoose.connect "myConnString"
        console.log 'Started connection, waiting for it to open'


        @db.connection.on 'open', () ->
            console.log 'Opened connection'
            callback()
    catch err
          console.log 'Setting up failed:', err.message
tearDown: (callback) ->
    console.log 'In tearDown'
    try
        console.log 'Closing connection'
        @db.disconnect()
        callback()
    catch err
        console.log 'Tearing down failed:', err.message
"get tasks" : (test) ->
    console.log 'running first test'
    Task.find {}, (err, result) ->
        if not err
            console.log 'results' + result
            test.ok(result)
        else
            console.log 'error' + err   
        test.ifError(err)
        test.done()
  • 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-05-28T05:27:53+00:00Added an answer on May 28, 2026 at 5:27 am

    I’ve ported the test script from Coffee Script to JavaScript and ran it in NodeUnit, see below.

    There were two things I have changed though. First, instead of:

    @db.connection.on 'open', () ->
    

    I did this (in Coffee Script):

    mongoose.connection.on 'open', () ->
    

    Second, I’ve reversed the order or registering the callback and making the connection.

    The resulting JavaScript:

    var mongoose = require('mongoose');
    var models = require('./models');
    var Task = models.Task;
    
    var db;
    
    module.exports = {
        setUp: function(callback) {
            try {
                //db.connection.on('open', function() {
                mongoose.connection.on('open', function() {
                    console.log('Opened connection');
                    callback();
                });
    
                db = mongoose.connect('mongodb://localhost/test_1');
                console.log('Started connection, waiting for it to open');
            }
    
            catch (err) {
                console.log('Setting up failed:', err.message);
            }
        },
    
        tearDown: function(callback) {
            console.log('In tearDown');
            try {
                console.log('Closing connection');
                db.disconnect();
                callback();
            }
    
            catch (err) {
                console.log('Tearing down failed:', err.message);
            }
        },
    
        getTasks: function(test) {
            console.log('running first test');
            Task.find({}, function (err, result) {
                if (!err) {
                    console.log('results' + result);
                    test.ok(result);
                } else {
                    console.log('error' + err);
                }
    
                test.ifError(err);
                test.done();
            });
        }
    };
    

    Models.js

    var mongoose = require('mongoose');
    
    var TaskSchema = new mongoose.Schema({
        field1: String,
        field2: Number
    });
    
    module.exports.Task = mongoose.model('Task', TaskSchema);
    

    And the resulting output:

    $ ~/node_modules/nodeunit/bin/nodeunit test.js 
    
    test.js
    Started connection, waiting for it to open
    Opened connection
    running first test
    results
    In tearDown
    Closing connection
    ✔ getTasks
    
    OK: 2 assertions (198ms)
    

    I do have to note that when MongoDB was not running/not connectable, the tests were failing like you stated. So you might want to check your connection string as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get the code found here: http://snipplr.com/view/26643/mbprogresshud-with-an-asynchronous-nsurlconnection-call/ to work in my
Since Node is asynchronous, I'm having problems trying to get a callback to return
The following asynchronous C# code runs through a list of 7 URLs and tries
I'm trying to get the method data.SetValue(...) working in the asynchronous callback in method
How can we get data from the server in an asynchronous way in Flash?
We get the following error; The request was aborted: Could not create SSL/TLS secure
I get the following error when running my Visual Studio 2008 ASP.NET project (start
I get the following error when building my Windows Forms solution: LC.exe exited with
I'm still new to ExtJS and I cannot figure out how to get code-generated
This question came as a result out of: Asynchronous POST to server . To

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.