I am using mongoose as ODM for a nodejs-mongodb application.
Is my first nodejs application and I come from a non functional programming background.
Looking at the docs of mongoose you can find:
Kitten.find(function (err, kittens) {
if (err) return console.error(err);
console.log(kittens);
});
So great, here we have the find function as part of our model (Kitten) and it actually can find the document and retrieve it inside the callback function as "kittens", and in this case it uses console.log() .
But I want to know how this functional programming is used to assign this value into a variable (because I have this in another file in the models folder and I import the module with require)
I found another question about something similar asking for the ObjectId but they offer the same kind of answer when you use the callback to just use the console.log to print the answer and if we are honest this is not useful.
Since I come from a non functional programming background I was expecting something like:
var kitten = Kitten.find({name:'Silence'});
As far as I know if you assign a new variable inside the callback the scope of the variable is just within the callback function, the same with a return variable , even declaring a variable before the method is not gonna work.
Im sure there's something Im missing. This project is so big and I don't think they can't forget to offer a method to do this. I think there is something in the functional programming I am missing or I don't know.
So, how can achieve this ?
Thanks !
Edit: I dont know why the community is saying that my question is a possible duplicate of How to return the response from an asynchronous call? this question is more oriented to js ajax async in general and this question is oriented to a ODM framework and how to handle the result that it can be done through promises too and is a different thing