mongo-dart / mongo_dart

Mongo_dart: MongoDB driver for Dart programming language
https://pub.dev/packages/mongo_dart
MIT License
446 stars 98 forks source link

Regression in 0.8.1 `Db is in the wrong state: State.OPENING` #313

Closed SunlightBro closed 1 year ago

SunlightBro commented 1 year ago

Calling await Db(uriString).open(secure: true);

Stack trace

```sh #0 Db._masterConnectionVerified (package:mongo_dart/src/database/db.dart:211:7) #1 Db.masterConnection (package:mongo_dart/src/database/db.dart:281:38) #2 Db.getNonce (package:mongo_dart/src/database/db.dart:595:9) #3 MongoDbCRAuthenticator.authenticate (package:mongo_dart/src/auth/mongodb_cr_authenticator.dart:18:15) #4 Db.authenticate (package:mongo_dart/src/database/db.dart:737:25) #5 ConnectionManager._connect (package:mongo_dart/src/network/connection_manager.dart:97:18) #6 ConnectionManager.open (package:mongo_dart/src/network/connection_manager.dart:141:9) #7 Db.open (package:mongo_dart/src/database/db.dart:481:7) ```

I'm have a Azure Cosmos Db , with a specific Table I'm trying to connect to, using authMechanism=MONGODB-CR mongodb://foo-bar:******@foo-bar.mongo.cosmos.azure.com:<mongoDbPort>/<mongoDbTable>?authMechanism=MONGODB-CR

giorgiofran commented 1 year ago

I released 0.8.2 with a fix. Please, give it a try and let me know if it is OK.

giorgiofran commented 1 year ago

May you please give more details? Which method are you using to create the Db? Which version of MongoDb? Thanks

DevMensIT commented 1 year ago

May you please give more details? Which method are you using to create the Db? Which version of MongoDb? Thanks

Actually problem is im using provider continue refresh data from mongodb while inserting i'm getting that error [only happen when connection state is opening sometime its gets inserted record] :

await db.open().then((value) async {
        await collection.insert(data);
        notifyListeners();
      }).catchError((error) {
        print('Error connecting to MongoDB: $error');
      });

i'm now looking for another solution where database stay open always do you have solution regarding that ?

giorgiofran commented 1 year ago

Normally you maintain the same Db object. once that it has been opened, you reuse the same, unless it gets disconnected. In this case you have to close the Db and reopen again (or create a new one). When you are done with all the operations you can close the Db.

In the folder example of the sources there are many examples.

 var db = Db(defaultUri);
 await db.open();

 var collectionName = <Collection Name>;
 var collection = db.collection(collectionName);
  ...
 await db.close();
DevMensIT commented 1 year ago

Normally you maintain the same Db object. once that it has been opened, you reuse the same, unless it gets disconnected. In this case you have to close the Db and reopen again (or create a new one). When you are done with all the operations you can close the Db.

In the folder example of the sources there are many examples.

 var db = Db(defaultUri);
 await db.open();

 var collectionName = <Collection Name>;
 var collection = db.collection(collectionName);
  ...
 await db.close();

actually i'm trying to keep open my database connection using provider so it keep fetching data but problem is when i'm trying to insert data sometime its give me error and record not getting insert because of this error : MongoDart Error: Db is in the wrong state: State.OPENING

do you have any other solution for that ? i just want to keep live records fetch

giorgiofran commented 1 year ago

What is "provider"?

DevMensIT commented 1 year ago

What is "provider"?

provider is state management.

giorgiofran commented 1 year ago

Can you give me an example?

DevMensIT commented 1 year ago

Can you give me an example?

thank man for replaying i already found solution. here is provider package link : https://pub.dev/packages/provider