Search code examples
mongodbdartdart-async

MongoDart find with problems


I am implementing a MVC framework in Dart. I am implementing the find method and I want it to return the documents from that query.

The problem is that find() doesn't wait that the operation is performed and we need to bind a function inside then().

 static find(model, [params]){
     Db db = new Db("mongodb://127.0.0.1/dart");
     var models = [];
     db.open().then((o){
         return db.collection(model).find(params).forEach((d){
         models.add(d);
         });
     });
     return models;
}

Right now the return from find() is []. Do you know any way of returning the documents properly?


Solution

  •  static Future<List> find(model, [params]){
         Db db = new Db("mongodb://127.0.0.1/dart");
         var models = [];
         return db.open().then((o){
           db.collection(model).find(params).forEach((d){
             models.add(d);
           });
           return models;
         });
    }
    

    and use it like

    find(model, [p1, p2, p3]).then((m) => ...);