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