Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Promise.Await does not wait for the promise to be resolved (Meteor fw)

In Meteor framework, I thought that using Promise.Await is meant exactly to do that – to wait for the promise to be resolved. However, in the following code, the console logs Promise:

'projects.count.categories':  function () {
            if (Meteor.isServer) {
                try {
                    let result = Promise.await(Projects.rawCollection().aggregate(
                        [
                              {
                                $match: {  }
                              },
                              {
                                $group: { _id: "$category", count:{$sum:1} }
                              }
                        ]
                    ));
                    console.log(result.toArray())  //RETURNS PENDING

                    return result;
                }
                catch (e) {
                    throw e;

                }

            }
        },

I have not found what is wrong with the code in this case.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

toArray is what returns a promise (e.g. see this question). You’d need to do:

Promise.await(Projects.rawCollection().aggregate(/* ... */).toArray())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading