Answer the question
In order to leave comments, you need to log in
How to export find result from model in node.js?
Good day everyone I am
using MVC for the first time and trying to abstract the model as much as possible.
I don’t fully understand how to apply promises for the same result and which option is the best practice
Post.find {}, (err, posts) ->
if err
console.error err
exports.findPostsForMain = {posts: posts}
.limit 8
Answer the question
In order to leave comments, you need to log in
export is executed here and now, while find is a deferred action.
If you want to export, it is better to export a function that returns a promise
exports.findPostsForMain = function(){
return Post.find({}).limit(8).exec();
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question