Abe Miessler
Abe Miessler

Reputation: 85056

Called a stored javascript function from Mongoose?

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?

Upvotes: 3

Views: 3969

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151122

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.

Upvotes: 3

Related Questions