0

I'm trying to get all authors and books from database in SailsJS using following code:

module.exports = {

    all: function(req, res) {

        let title = 'All Authors'

        let authors = Author.find({}).then(function(results){ return results })
        let books = Book.find({}).then(function(results){ return results })

        sails.log(authors)

    }

}

I'm getting following output:

Promise {
    _bitField: 0,
    _fulfillmentHandler0: undefined,
    _rejectionHandler0: undefined,
    _promise0: undefined,
    _receiver0: undefined }

I feel like the sails.log function is running before Promise completion. Guide me how to return values from Promise.

Yousuf Iqbal Hashim
  • 925
  • 1
  • 11
  • 21
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – ponury-kostek Oct 25 '17 at 08:20

1 Answers1

0

You should wait for asynchronous operations to finish if you want to log the results. The way you have written your code it seems like you want to do this:

Promise.all([Author.find({}), Book.find({})]).then((results) => {
  let authors = results[0];
  let books = results[1];

  sails.log(authors)
});
guramidev
  • 2,228
  • 1
  • 15
  • 19