map函数完成后实现回调?

时间:2016-12-30 02:31:13

标签: javascript node.js

我对Javascript很新。我不明白所有这些不同的异步库。

我希望在完成所有这些计算后执行回调。目前,它按原样(空)发送finalized_restaurant_query对象,而不使用我插入的新数据。如何强制它插入数据然后调用回调?

(目前使用async-waterfall库)

我目前有以下内容:

function gather(res, matching_businesses) {


waterfall([
  function(callback){


let finalized_restaurant_query = {
  restaurants : { }
}

MongoClient.connect("mongodb://localhost:27017/restaurants", function(err, db) {
  matching_businesses.map((e => {
    if(err) { return console.dir(err); }

    let business_name = e['name']

    //Get the collection that all the data exists within
    const collection = db.collection('restaurant_meta_data');

    //Map over the Yelp data for the restaurants and cross-ref a match
    let regex_search = new RegExp(".*" + business_name + ".*", 'i')

    //Map over each item and push into associated restaurants menu
    collection.find({"name": {'$regex': regex_search} })
    .toArray(function(err, item) {
      try { 
        if (finalized_restaurant_query['restaurants'][business_name] == undefined) {
          finalized_restaurant_query['restaurants'][business_name] = {
            image: item[0]['logo'],
            items: [] //House individual restaurant menu items
          }
        } else {
          finalized_restaurant_query['restaurants'][business_name]['items'].push(item)
        }
      } catch(err) {
        console.log('err on ', business_name)
        console.log(item)
      }
    });
  })
});

callback(null, finalized_restaurant_query);

  },

  function(arg1, callback){
    console.log(arg1)
    callback(null, arg1);
  }

], function (err, result) {
    return res.json(result);
});

} 

1 个答案:

答案 0 :(得分:0)

希望它可以帮到你

function gather(res, matching_businesses) {
    waterfall([
        function(callback){
            let finalized_restaurant_query = {
                restaurants : { }
            }

            MongoClient.connect("mongodb://localhost:27017/restaurants", function(err, db) {
                matching_businesses.map((e => {
                    if(err) { 
                        console.dir(err); 
                        return callback(err, {});
                    }

                    let business_name = e['name']

                    //Get the collection that all the data exists within
                    const collection = db.collection('restaurant_meta_data');

                    //Map over the Yelp data for the restaurants and cross-ref a match
                    let regex_search = new RegExp(".*" + business_name + ".*", 'i')

                    //Map over each item and push into associated restaurants menu
                    collection.find({"name": {'$regex': regex_search} })
                        .toArray(function(err, items) {
                            try { 
                                if (finalized_restaurant_query['restaurants'][business_name] == undefined) {
                                  finalized_restaurant_query['restaurants'][business_name] = {
                                    image: items[0]['logo'],
                                    items: [] //House individual restaurant menu items
                                  }
                                } else {
                                  finalized_restaurant_query['restaurants'][business_name]['items'].push(items)
                                  callback(null, finalized_restaurant_query);
                                }
                            } catch(err) {
                                console.log('err on ', business_name)
                                console.log(item)
                                callback(err, {});
                            }
                        });
                    });
                });
            });
        },
        function(arg1, callback){
            console.log(arg1)
            callback(null, arg1);
        }
    ], function (err, result) {
        if (err){
            return res.json({});
        }
        return res.json(result);
    });
}