resolving a promise using mongodb and nodejs

2019-07-31 13:56发布

问题:

Hello I am new to nodejs and mongodb, i am having trouble resolving my first promise after the second promise has been resolved. I can see the result of my second promise after it is resolved. Here is my code

 var getShows = function () {

    return new Promise(function (resolve, reject) {

    usersdb.find(function (err, result) {
        if(err) return console.error(err);

        var usersFromCall = result;

        var task = function (users) {
            return new Promise(function (resolve, reject) {

                var user = {
                    'name': '',
                    'pages': []
                }

                user.name = users.show;
                console.log(users);

                pagedb.find({'show' : user.name}, function (err, resp) {

                    for(var j = 0 ; j <resp.length; j ++){
                        var pages = { 'pageId': ''};
                        pages.pageId = resp[j].pageId;

                        user.pages.push(pages);
                    }
                    console.log(user);

                    resolve({show: user});
                })

            });

        };

        var actions = usersFromCall.map(task);
        return Promise.all(actions);
    }).then(function () {
        resolve()
    })
 });

};

do i resolve the first promise in a then function after the find?

回答1:

The following should work:

var getShows = function () {

    return new Promise(function (resolve, reject) {

        usersdb.find(function (err, users) {
            if (err) return console.error(err);

            var task = function (user) {
                return new Promise(/* ... */);
            };

            var actions = users.map(task);
            Promise.all(actions).then(resolve, reject);
        });

    });

};

getShows().then(function (results) {
    // Prints the result of each task as an array
    console.log(results);
});


回答2:

Looking at your code, it seems .find returns a Promise. So, just for the sake of avoiding the Promise constructor anti-pattern please try the following, I believe it will produce the correct result

var getShows = function() {
    return usersdb.find()
    .then(result => 
        Promise.all(result.map(
            users => 
                pagedb.find({
                    show: users.show
                })
                .then(resp => ({
                    show: {
                        name: users.show,
                        pages: resp.map(item => ({pageId: item.pageId}))
                    }
                }))
            )
        )
    );
};

or the ES5 version

var getShows = function getShows() {
    return usersdb.find().then(function (result) {
        return Promise.all(result.map(function (users) {
            return pagedb.find({
                show: users.show
            }).then(function (resp) {
                return {
                    show: {
                        name: users.show,
                        pages: resp.map(function (item) {
                            return { pageId: item.pageId };
                        })
                    }
                };
            });
        }));
    });
};