Called a stored javascript function from Mongoose?

javascript, mongodb, mongoose

Solution

Most mongoose calls do not return in-line like this, but rather expect a callback to be passed in to process the results.

Completely untested, but you probably want something like:

mongoose.connection.db.eval("getTotals()", function(err, retVal) {
   console.log(retVal)
});

And in the real world, assign your result to a `var` outside of that scope or whatever you want to do.

Problem

I have created a stored javascript function in my MongoDB instance that counts the number of records in each collection. If I go to my mongo shell and type: ``` > db.eval("getTotals()"); ``` it works as expected. if I try to call it through mongo like so: ``` totals = mongoose.connection.db.eval("getTotals()"); console.log(totals); ``` `undefined` gets logged. Does anyone see what I am doing wrong here?

Original source