2016-12-30 55 views
0

我是Javascript的新手。我不明白所有這些不同的異步庫。地圖功能完成時執行回調嗎?

當所有這些計算完成時,我想讓回調執行。目前,它將原樣發送finalized_restaurant_query對象(空),而不使用插入到其中的新數據。我怎樣才能強制它的數據插入,然後調用回調?

(目前使用異步瀑庫)

目前,我有以下幾點:

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); 
}); 

} 

回答

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); 
    }); 
}