1

I've a simple method which fetches some data from MongoDB:

import 'package:mongo_dart/mongo_dart.dart';

Future find() {
  return _db.open().then((_) {
    return _db.collection('foo').find().toList();
  }).then((val) {
    _db.close();
    return val;
  });
}

It's working if I'm calling this method once. Calling it twice in a row results in a NoSuchMethodError: method not found: 'query'.

Could anyone explain the problem?

Here is the full stacktrace:

Uncaught Error: The null object does not have a method 'query'.

NoSuchMethodError: method not found: 'query'
Receiver: null
Arguments: [Instance of 'MongoQueryMessage']
Stack Trace: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      Db.queryMessage (package:mongo_dart/src/database/db.dart:174:28)
#2      Cursor.nextObject (package:mongo_dart/src/database/cursor.dart:68:29)
#3      Cursor._nextEach (package:mongo_dart/src/database/cursor.dart:102:15)
#4      Cursor.forEach (package:mongo_dart/src/database/cursor.dart:122:14)
#5      Cursor.toList (package:mongo_dart/src/database/cursor.dart:128:24)
#6      DbConnection.find.<anonymous closure> (package:schafkopfer_server/src/schafkopfer/data.dart:49:52)
#7      _rootRunUnary (dart:async/zone.dart:730)
#8      _RootZone.runUnary (dart:async/zone.dart:864)
#9      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488)
#10     _Future._propagateToListeners (dart:async/future_impl.dart:571)
#11     _Future._complete (dart:async/future_impl.dart:317)
#12     Future.forEach.nextElement (dart:async/future.dart:303)
#13     _rootRunUnary (dart:async/zone.dart:730)
#14     _RootZone.runUnary (dart:async/zone.dart:864)
#15     _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:488)
#16     _Future._propagateToListeners (dart:async/future_impl.dart:571)
#17     _Future._completeWithValue (dart:async/future_impl.dart:331)
#18     _Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:393)
#19     _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:23)
#20     _asyncRunCallback (dart:async/schedule_microtask.dart:32)
#21     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:128)

I opened a issue for mongo_dart.

4

2 回答 2

2

I believe you are mixing asynchronous and synchronous code and that would not work in most circumstances.

Method find returns Future, but you are trying call find second time in sync mode. Would you call find second time from within then clause, your code would work.

main() {
  var mongoTest = new MongoTest();
  mongoTest.find().then((x) {
    print('first find $x');
    mongoTest.find().then((x) => print('second find $x'));
  });
}

So mongo_dart Db instance can be opened after closing, but it hardly make sense in that example.

If you are trying to do server side of web stack I would recommend you to look at existing frameworks. Redstone for example.

http://redstonedart.org/2014/07/14/managing-database-connection-with-redstondart/

于 2014-07-25T04:40:59.803 回答
1

According to the Gist you linked to in the bug report the only difference between the 1st and the 2nd call is, that you use the same db instance. I guess the instance can't be reopened after it was closes. Creating a new instance every time before opening should solve the problem.

When the db connection is used intensively it might be better to keep it open.

于 2014-07-24T18:35:39.527 回答