为什么Mongo Dart没有出现错误?

时间:2015-09-20 20:26:21

标签: mongodb asynchronous dart

我试图使用Dart的mongo模型进行异步编程

我正在查看DbCollection的源代码,看来DbCollection.find()返回一个Stream https://github.com/vadimtsushko/mongo_dart/blob/master/lib/src/database/dbcollection.dart

我想将响应转换为地图列表,所以即时执行以下操作:

try {
  finder = await collection.find(query);
} catch(e) {
  print(e);
}

try {
  list = await finder.toList();
} catch(e) {
  print(e);
}

问题: 执行轰炸了collection.find 2.没有错误被困

问题:我需要使用这种api有不同的方法吗?

1 个答案:

答案 0 :(得分:1)

查看包readme。有一些例子。

这样的事情应该做:

var collection = db.collection('user');
await collection.find(where.lt("age", 18)).toList();

但实际上,即使使用超级流媒体await,它也应该有效。 我用你的代码片段做了一个简单的例子,它对我有用

import 'package:mongo_dart/mongo_dart.dart';
main() async {
  Db db = new Db('mongodb://127.0.0.1:27017/mongo_dart-test','sample_test');
  DbCollection newColl;
  await db.open();
  newColl = db.collection("new_collecion");
  await newColl.remove();
  await newColl.insertAll([{"a":1}]);
  var finder;
  try {
    finder = await newColl.find();
  } catch(e) {
    print(e);
  }
  List list;
  try {
    list = await finder.toList();
  } catch(e) {
    print(e);
  }
  print(list);
  await db.close();
}