A
A
Anton Ratnik2016-02-15 15:36:45
MongoDB
Anton Ratnik, 2016-02-15 15:36:45

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

Please suggest the best solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MNB, 2016-02-15
@Ratnick

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();
};

As far as I remember, exec in Mongoose returns a promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question