Andries Heylen Andries Heylen - 6 months ago 10
Node.js Question

I never get in the callback function, nodejs just stops

I always used nodejs together with express, but now I try to run the following script in nodejs (just by calling in the terminal "node myscript.js")

postModel is a mongoose model, doing a query on a mongodb.

'use strict'
var postModel = require(__dirname + '/../model/post').postModel;
myfunction();
function myfunction() {
console.log("before call");
postModel
.find({isArchived: false})
.exec(function(err,result){
console.log("result is ok")
})
};


so what I don't understand is why I never see "result is ok" in my terminal.

I also tried this with the async and wait.for library, but also there my script just stops before the async function is executed and a result is returned.

I think I just miss something in what I am doing, but no idea what...

Answer

You need to call mongoose.connect() to actually connect to a database.