0

我正在co运行一个生成器函数,该函数对数据进行一些抓取和清理。但是,在循环之后,我永远不会到达代码的某个部分。这是我的代码的样子:

function*(){
  var url = "mongodb://" + config.host + ":" + config.port + "/" + config.db;
  var db = yield MongoClient.connect( url );
  for( var establishment in [ {"establishment_id": 16} ] ){
    var establishment_type = establishment.id;
    var response = yield getRestaurants( establishment_type );
    var restaurants = JSON.parse( response.body ).restaurants;
    // here we create our restaurants in "saveable" form
    var saveableRestaurants = [];
    for(var i = 0; i < restaurants.length; i++){
        var saveableRestaurant = restaurants[i].restaurant;
        saveableRestaurant._id = restaurants[i].restaurant.R.res_id;
        // Remove unwanted fields
        //... code removed for brevity ...
        // convert cuisine string into its appropriate id form
        var cuisines = saveableRestaurant.cuisines.split(",");
        var ids = [];
        for( var i = 0; i < cuisines.length; i++ ){
            console.log("LOOKING UP ID FOR ", cuisines[i]);
            var match = yield db.collection(CUI).findOne({"cuisine_name":cuisines[i].trim()});
            console.log("ID FOR ", cuisines[i], " IS", match.cuisine_id);
            ids.push( id );
        }
        // I NEVER REACH THIS LINE!!!!
        console.log( "ALL IDS ", ids );
    }
  }
  db.close();
}  

我从来没有在函数结束时到达控制台语句

4

3 回答 3

2

很可能存在您没有注意到的异步异常。你应该使用

function*() {
  var url = "mongodb://" + config.host + ":" + config.port + "/" + config.db;
  var db = yield MongoClient.connect( url );
  try {
    for (var establishment of [ {"establishment_id": 16} ]) {
      var establishment_type = establishment.id;
      var response = yield getRestaurants( establishment_type );
      var restaurants = JSON.parse( response.body ).restaurants;
      // here we create our restaurants in "saveable" form
      var saveableRestaurants = [];
      for (var i = 0; i < restaurants.length; i++) {
        var saveableRestaurant = restaurants[i].restaurant;
        saveableRestaurant._id = restaurants[i].restaurant.R.res_id;
        // Remove unwanted fields
        //... code removed for brevity ...
        // convert cuisine string into its appropriate id form
        var cuisines = saveableRestaurant.cuisines.split(",");
        var ids = [];
        for (var i = 0; i < cuisines.length; i++) {
          console.log("LOOKING UP ID FOR ", cuisines[i]);
          var match = yield db.collection(CUI).findOne({"cuisine_name":cuisines[i].trim()});
          console.log("ID FOR ", cuisines[i], " IS", match.cuisine_id);
          ids.push( id );
        }
        console.log( "ALL IDS ", ids );
      }
    }
  } finally {
    db.close();
  }
}

也不要忘记.catch(e => console.error(e))在返回的承诺上加上一个co.

于 2016-03-07T15:47:41.183 回答
1

在浏览了 MongoDB 文档后,我找到了解决方案:

var matches = db.collection( CUI ).find({"cuisine_name" : { "$in" : cuisines }});
while( yield matches.hasNext() ){
  var match = yield matches.next();
  ids.push( match.cuisine_id );  
}
于 2016-03-07T15:51:27.743 回答
-1

您正在打开 db connection 并且您正在尝试使用 db connection 进行操作,因此您可以尝试使用 async.each 并在完成每个功能后关闭连接,我发现这是问题所在。

于 2016-03-07T15:31:25.087 回答