是否可以使用飞镖连接到本地MongoDB? [英] Is it possible to connect to local MongoDB with flutter dart?

查看:65
本文介绍了是否可以使用飞镖连接到本地MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我尝试连接到本地MongoDB时,都会不断收到此异常:SocketException:操作系统错误:连接被拒绝,errno = 111,地址= localhost,端口= 38748

Everytime I try to connect to my local MongoDB, I keep getting this Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 38748

有趣的事实:每次尝试后,异常中的端口都会增加2,并且始终是错误的(我什至在启动服务器时,端口接下来都会抛出异常)

Fun fact: the port in the exception increments by two after each try and is always wrong (I even started the server with the port throwing the exception next)

MongoDB服务器正在运行-我NETWORK [initandlisten]等待端口27017上的连接

MongoDB server is running - I NETWORK [initandlisten] waiting for connections on port 27017

已设置依赖性-

依赖关系:mongo_dart:^ 0.3.5扑:sdk:颤抖

dependencies: mongo_dart: ^0.3.5 flutter: sdk: flutter

import 'package:mongo_dart/mongo_dart.dart' show Db, DbCollection;
class DBConnection {

  static DBConnection _instance;

  final String _host = "localhost";
  final String _port = "27017";
  final String _dbName = "debtservice";
  Db _db;

  static getInstance(){
    if(_instance == null) {
      _instance = DBConnection();
    }
    return _instance;
  }

  Future<Db> getConnection() async{
    if (_db == null){
      try {
        _db = Db(_getConnectionString());
        await _db.open();
      } catch(e){
        print(e);
      }
    }
    return _db;
  }

  _getConnectionString(){
    return "mongodb://$_host:$_port/$_dbName";
  }

  closeConnection() {
    _db.close();
  }

}

我已经尝试过以普通飞镖代​​码运行此代码,并且可以运行.

I already tried to run this code in plain dart and it runs.

推荐答案

好,我现在解决了这个问题...

Ok, I fixed the problem now...

我必须使用家用设备的无线LAN适配器IP,并使用--bind_ip -ip-启动mongodb.还必须在连接字符串中提供ip.

I had to take the Wireless LAN-Adapter IP of the my home device and start the mongodb with --bind_ip -ip-. The ip must also be provided in the connection string.

仍然要感谢MichaelM.

Still Thanks to MichaelM.

这篇关于是否可以使用飞镖连接到本地MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆