logo
down
shadow

How can i test async functions using express, mongoose and nodeunit?


How can i test async functions using express, mongoose and nodeunit?

By : user2954965
Date : November 22 2020, 10:33 AM
this will help You can pass a callback parameter to the getData function which executes when Mongoose has returned the data.
code :
var getData = function (req, res, cb) {
  Category.find({}, function (err, docs) {
    if (!err) {
      res.json(200, { categories: docs });
    } else {
      res.json(500, { message: err });
    }
    cb();
  });
};
var request  = httpMocks.createRequest({
  method: 'GET',
  url: '/category/list',
  body: {}
});
var response = httpMocks.createResponse();
getData(request, response, function() {
  console.log(response._getData());
  test.done();
});


Share : facebook icon twitter icon
How to test POST-requests with NodeUnit and Express?

How to test POST-requests with NodeUnit and Express?


By : Pirx
Date : March 29 2020, 07:55 AM
I wish did fix the issue. You could try this library instead of nodeunit: https://github.com/masylum/testosterone
It's built specifically to test web apps over http.
Express response doesn't work when using mongoose and async

Express response doesn't work when using mongoose and async


By : Koteibb
Date : March 29 2020, 07:55 AM
With these it helps When you say "I got a timeout error" do you mean mocha failed your test for taking too long? If so that is probably a problem in your mocha test itself not calling done() correctly. The above code looks OK to me and I think it should work. Some misc points:
Whenever you have this pattern:
code :
user.save(function(err) {
  callback(err);
});
user.save(callback);
User.update({}, {$push: {messages: req.body.message}}, function (error) {...});
Unable to define callback for async.forEachOf, express, mongoose

Unable to define callback for async.forEachOf, express, mongoose


By : Ndudi Uti
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further forEachOf(obj, iterator, [callback]) So, try to change code like below
code :
                console.log("There "+sendData);
            });
        } // end of "if(dbName!='local')"
    }, function(err) {
        console.log("Here "+sendData);
        res.send(sendData);
    });
app.get('/tasks', function (req, res) {
    var connection = mongoose.createConnection('mongodb://localhost/temp');
    connection.on('open', function() {
        new Admin(connection.db).listDatabases(function(err, result) {
            console.log('listDatabases succeeded');
            // database list stored in result.databases 
            var dbs = result.databases
                .map(function(db) { return db.name}) // transform to ['db1', 'db2', 'local']
                .filter(function(name) { name != 'local' }); // remove 'local'

            async.map(dbs, function(db, callback) {
                var conn = mongoose.createConnection('mongodb://localhost/' + db);
                var Meta = new mongoose.Schema({
                    originalname : { type: String, trim: true }
                });
                conn.model('meta', Meta).find({ task: { $ne: null } }, function(err, doc) {
                    // probably we need close temporary conn; I don't work with mongodb
                    // If we don't need to close then we call callback instead function(err, doc) and transfrom if finally-function
                    callback(err, (err) ? null : {
                        "task_hash" : db,
                        "task_info" : doc[0].toObject()
                    });
                }); 
            }, 
            // async finally function 
            function(err, results) {
                if (err)
                    return ...; // Show error to user

                console.log(results);
                res.json(results);
            }); // end of async  
        });
    }); // end of connection.on('open', ...
});
Express + Mongoose: async/await returning undefined

Express + Mongoose: async/await returning undefined


By : Junaid Ejaz
Date : March 29 2020, 07:55 AM
I wish this helpful for you The reason: Your controller's getAll method doesn't return any value.
You should do smth like this:
code :
exports.getAll = async (criteria) => {
    // Return value!
    return await model.find(criteria, '-__v');
}
Nodeunit test hangs for async db call ORMnomnom

Nodeunit test hangs for async db call ORMnomnom


By : Reni
Date : March 29 2020, 07:55 AM
it should still fix some issue Add a tearDown method to your exports to close mongoose's connection pool. As long as it's still open the test won't exit.
You don't show how you're opening your mongoose connection, but something like this:
Related Posts Related Posts :
  • How to change add and remove active class in JavaScript
  • how to toggle a specific div in javascript
  • How to get all the values from multiple keys of an array of object?
  • jQuery bind() unbind() and on() and off()
  • regular expression to find DD Month YYYY from a string
  • How to upload files from web client (HTML/jQuery form) to your Dropbox folder
  • Backbone history and require.js issue
  • Confusion over Classical and Prototypal Inheritance Javascript
  • How to take screen shot of current webpage using Javascript/JQuery?
  • Store meridiem in a variable
  • class declared inside closure vs standard class without closure
  • javascript confirm box always return true
  • dynamically changing the size of font size based on text length using css and html
  • Node.js - Getting the host IP address while on Amazon EC2
  • Confusing CSS, can anyone explain please
  • How to install v8-profiler on Windows 8 (64 bit) using npm?
  • Resolve promise based on another promise
  • google maps adding overlay layer above UI and markers
  • Style specific letter?
  • RangeError: Maximum call stack size exceeded with array.slice
  • node rest client get with local variable
  • If condition not executes while mouse move fastly
  • Count the number of displayed elements in a HTML list
  • Locate JavaScript source code in Emacs
  • Clean, Modular Code vs MV* Frameworks
  • infinite scroll without loading image
  • Backbone: reverse collection order with comparator
  • What do printers ignore?
  • jQuery UI .tabs() Contentless tab?
  • Execute Javascript alert() after page (visually) loaded
  • JavaScript - duplicating array doesn't work
  • Excessive clickable area below image
  • JavaScript Regex: Replace |b| with <b>
  • Unexpected value change in 2D array in JavaScript
  • Function doesnt see parameter as a array of objects
  • jQuery fetch keypress event on chrome and IE
  • How to enable jquery validate localization?
  • Cassandra map collection returned by node.js Helenus looks odd
  • angular ng-repeat with multiple filter options
  • Selecting Children without ID or Class names
  • How to uncheck a group of checkboxes when another checkbox is checked
  • Is hiding content by Javascript or jQuery Worth trying
  • Load XUL resource using javascript
  • XML to HTML text area from server file system
  • set focus() on textbox on form onload
  • es lint '' is assigned a value but never used' eventhough i have used it
  • Can "name" attribute be used for custom VueJS components
  • Get innerHTML of content when it is clicked
  • HTTPS causes CSS animations to not load? Very confused
  • How to hide other div apart from touched div to the band
  • Is global variable assignment atomic on NodeJS?
  • Datepicker onchangemonthyear beforeshowday
  • trying to render html files using jade but it still adresses it as a jade
  • Google Analytics - Understanding and Dissecting the Snippet: What is | | [ ];
  • Replacing "[aA09.b]." to "[aA09.b]\n" in in JavaScript
  • Why wrap a function definition in an immediate function?
  • Enable / disable jQuery script on switching to different tab in a HTML page
  • how can find return variable value outside anonymous function in node js mysql query function
  • Converting 1 to 0001 in javascript array
  • svg viewbox should not resize the text fontSize?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org