user310563
user310563

Reputation: 13

Error connecting to mongoDB database from dart web

I'm unable to connect to a mongodb database from a dart application. I can connect only from terminal and from Compass

I've tried to run a simple example of connecting mongodb database from Dart (see the simple code below):

import 'package:mongo_dart/mongo_dart.dart';

void main() async {

Db db = new Db("mongodb://localhost:27017/test");
await db.open(); 
print('Connection succeeded!');
//the test database is accessible from Compass or terminal
//localhost:27017 works well   
}

await db.open() throws that error:
dart_sdk.js:5822 Uncaught Error: Unsupported operation: Socket 
constructor
at Object.dart.throw (dart_sdk.js:4537)
at Function._connect (dart_sdk.js:52279)
at Function.connect (dart_sdk.js:52264)
at mongo_dart._Connection.new.connect (connection.dart:56)
at mongo_dart._ConnectionManager.new._connect 
(connection_manager.dart:23)
at _connect.next (<anonymous>)
at runBody (dart_sdk.js:22269)
at Object.async.async (dart_sdk.js:22297)
at mongo_dart._ConnectionManager.new.[_connect] 
(connection_manager.dart:22)
at connection_manager.dart:58
at dart_sdk.js:23887

Upvotes: 0

Views: 643

Answers (1)

Nate Bosch
Nate Bosch

Reputation: 10925

mongo_dart.dart imports dart:io and therefore cannot be used on the web. This package mentions that is is server side.

With the latest build_web_compilers this should be a build time error instead of a runtime error.

You'll need to find, or write, a package for mongoDB that uses dart:html instead of dart:io.

Upvotes: 1

Related Questions