从Dart Web连接到mongoBD数据库时出错

时间:2019-05-28 18:02:39

标签: mongodb dart

我无法从dart应用程序连接到mongodb数据库。 我只能从终端和指南针进行连接

我尝试运行一个从Dart连接mongodb数据库的简单示例(请参见下面的简单代码):

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

1 个答案:

答案 0 :(得分:0)

mongo_dart.dart imports dart:io,因此无法在网络上使用。该软件包提到的是服务器端。

对于最新的build_web_compilers,这应该是构建时错误而不是运行时错误。

您需要查找或编写使用dart:html而不是dart:io的mongoDB软件包。