简体   繁体   中英

Not getting result in node js, mongo db using promise in loop

I am new in nodejs and mongodb. Its really very confusing to use promise in loop in nodejs for new developer. I require the final array or object . which then() give me final result. Please correct this.
I have a controller function described below.

let League = require('../../model/league.model');
let Leaguetype = require('../../model/leagueType.model');
let Leaguecategories = require('../../model/leagueCategories.model');


let fetchLeague = async function (req, res, next){
    let body = req.body;
    await mongo.findFromCollection(Leaguetype)
    .then(function(types) {
        return Promise.all(types.map(function(type){ 
            return mongo.findFromCollection(Leaguecategories, {"league_type_id": type._id})
            .then(function(categories) {
                return Promise.all(categories.map(function(category){
                    return mongo.findFromCollection(League, {"league_category_id": category._id})
                    .then(function(leagues) {
                        return Promise.all(leagues.map(function(league){
                            return league;
                    }))
                    .then(function(league){
                        console.log(league);
                    })
                    })
                }))
            });
        }))

    })
    .then(function(final){
      console.log(final);
    })
    .catch (error => {
      console.log('no',error);
  })
}

mongo.findFromCollection function is looking like this.

findFromCollection = (model_name, query_obj = {}) => {
        return new Promise((resolve, reject) => {
            if (model_name !== undefined && model_name !== '') {
                    model_name.find(query_obj, function (e, result) {

                    if (!e) {
                        resolve(result)
                    } else {
                        reject(e);
                    }
                })
            } else {
                reject({ status: 104, message: `Invalid search.` });
            }
        })
    }

and here is my model file

var mongoose = require('mongoose');
const league_categories = new mongoose.Schema({
        name: {
          type: String,
          required: true
        },
        active: {
          type: String,
          required: true
        },
        create_date: {
          type: Date,
          required: true,
          default: Date.now
        },
        league_type_id: {
          type: String,
          required: 'league_type',
          required:true
        }
      })

module.exports = mongoose.model('Leaguecategories', league_categories)

First i recommend you stop using callbacks wherever you can, its a bit dated and the code is much harder to read and maintain.

I re-wrote your code a little bit to look closer to what i'm used to, this does not mean this style is better, i just personally think its easier to understand what's going on.

async function fetchLeague(req, res, next) {
    try {
        //get types
        let types = await Leaguetype.find({});

        //iterate over all types.
        let results = await Promise.all(types.map(async (type) => {
            let categories = await Leaguecategories.find({"league_type_id": type._id});
            return Promise.all(categories.map(async (category) => {
                 return League.find({"league_category_id": category._id})
            }))
        }));

        // results is in the form of [ [ [ list of leagues] * per category ] * per type ]
       // if a certain category or type did not have matches it will be an empty array.
        return results;
    } catch (error) {
        console.log('no', error);
        return []
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM