Search code examples
node.jsmongodbcoffeescriptmongooserestify

MongoDb and Nodejs Server Response


In my javascript class, i have a function that return a list of things like so,

//ProjectClass.js
getProjectList: () =>
    @Project.find (err, projects) =>
        console.log(projects)
        return projects

However, whenever I try to send a server response from nodejs

//App.js
project = new projectSchema.Project()
res.send(project.getProjectList())

I get the following as a response

{
    "options": {
        "populate": {}
    },
    "_conditions": {},
    "_updateArg": {},
    "op": "find"
}

Ironically, if I pass the res object to my getProjectList and send server response from my getProjectList function then everything works just fine.

Thanks for the help!


Solution

  • @Project.find is an asynchronous function, calling your anonymous callback once the find results are available. getProjectList returns the result of the last synchronous statement executed in that function which is the return value of @Project.find. That's not the projects array your callback returns but the query object you see in your response.