Atlas mongoDB cloud service with dart package mongo_dart cause Connection Exception

559 Views Asked by At

I'm trying to write into Atlas mongoDB cloud service with dart package mongo_dart.

This is how to connect to:

  static const String uri =
      "mongodb+srv://User:[email protected]/DbName?retryWrites=true&w=majority&tls=true";

  Future<Db> connectToDatabase() async {
    var db = await Db.create(uri);
    await db.open(secure: true);
    print('Connected to database');
    return db;
  }

And this is how to update my data:

await coll.update(
            {
              "symbol": symbol,
            },
            {
              r"$addToSet": {"pens": pen.toJson()}
            },
          );

I've got this error rapidly:

Unhandled exception:
MongoDB ConnectionException: connection closed: The socket connection has been reset by peer.
Possible causes:
- Trying to connect to an ssl/tls encrypted database without specifiyng
  either the query parm tls=true or the secure=true parameter in db.open()
- Others

And sometimes this one:

Unhandled exception:
MongoDB ConnectionException: connection closed: SocketException: Read failed (OS Error: Connection reset by peer, errno = 104), address = ????.e0nro.mongodb.net, port = 52508

In Atlas panel I added the IP address to access the database globally

0.0.0.0/0  (includes your current IP address)   access  Active

How can I solve these errors?

0

There are 0 best solutions below