choyoung kim
choyoung kim

Reputation: 31

Node.js Promise with mongoose

I have difficulty using Promise.
I want to get data from one more tables in mongodb.
but I fell in callback hell, So I tried to solve this but I couldn't.
What should I do? the result came out 'undefined'.

Many Thanks,

let mongoose         = require('mongoose');
mongoose.Promise     = global.Promise;

....

exports.Recommend = (id) => {
    User.find({User_id: myId})
        .then((result) => {
            return Promise.resolve(result[0].age)
        }).then(age => {
        return new Promise((resolve,rejject)=>{
            resolve(User.find()
                .select('User_id')
                .where('age').equals(age))
            })
        }).then((Users_id) => {
            Users_id.forEach((user, idx, arr) => {
                Count.find()
                 .select('board_id')
                 .where('User_id').equals(user.User_id)
                 .exec((err, items) => {
                     return new Promise((resolve,reject)=>{
                         resolve(
                             items.forEach((post, idx, arr) => {
                             posts.push(post.board_id)
                         }))
                     })
                })
        })
    }).then(()=>{
        console.log("posts:"+posts);
    })
}

Upvotes: 1

Views: 74

Answers (2)

Bergi
Bergi

Reputation: 664538

Avoid Promise.resolve, avoid using the new Promise constructor like Promise.resolve, avoid the Promise constructor antipattern, and avoid forEach, and don't forget to return the promise chain from your function:

exports.Recommend = (id) => {
    return User.find({User_id: myId}).then(result => {
        return User.find()
            .select('User_id')
            .where('age')
            .equals(result[0].age));
    }).then(user_ids => {
        return Promise.all(user_ids.map((user, idx, arr) => {
            return Count.find()
                 .select('board_id')
                 .where('User_id').equals(user.User_id)
                 .exec()
                 .then(posts => posts.map(post => post.board_id));
        }));
    }).then(board_ids => {
        console.log("posts:"+board_ids);
    })
}

Upvotes: 1

Arif Khan
Arif Khan

Reputation: 5069

You have the problem with 3rd .then, I would like to recommend you to use Promise.all function to run the parallel database query. Following example may help you

exports.Recommend = (id) => {
    User.find({
            User_id: myId
        })
        .then((result) => {
            return User.find()
                .select('User_id')
                .where('age').equals(result[0].age)
        }).then((Users_id) => {
            return Promise.all(Users_id.map((user, idx, arr) => {
                return Count.find()
                    .select('board_id')
                    .where('User_id').equals(user.User_id)
            }));
        }).then((Users_id) => {
            Users_id.forEach(items => {
                items.forEach(post => {
                    posts.push(post.board_id)
                })
            })
        }).then(() => {
            console.log("posts:" + posts);
        })
}

Upvotes: 0

Related Questions