can't connect to MongoDB with flutter/dart - mongodb

I have an app I am building in flutter and want to connect it to a database in mongo, but can't for the life of me get it to connect, here is a simple dart code to troubleshoot:
it always returns the error:
Unhandled exception:
MongoDB ConnectionException: Could not connect to 127.0.0.1:27017
HandshakeException: Connection terminated during handshake
import 'package:mongo_dart/mongo_dart.dart';
import 'dart:io';
//Platform.environment['MONGO_DART_DRIVER_HOST']
//final mongoURI = Platform.environment['MONGO_DART_DRIVER_HOST'];
final url = "mongodb://127.0.0.1:27017/labelmask";
main() async {
Db _db = new Db(url);
await _db.open(secure: true);
print('connected');
}
Here is what I tried:
-I have tried using dart:io and get Platform.environments, but returned null
-Tried to connect to admin db instead of 'labelmask', same error
-I started first mongod and then mongo
-I can connect to this Database using python(pymongo) and from Compass GUI
-I can run mongo commands in terminal so I know it is connected, just not with dart

Related

how can i solve UnhandledPromiseRejectionWarning: MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

Before I start node.js, I already did mongod and mongo on PowerShell (I'm using Windows) with no error.
db.js code:
import mongoose from "mongoose";
mongoose.connect("mongodb://127.0.0.1:27017/wetube");
but every time I start, this error occurs.

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
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.

Connect to MongoDB Atlas with mongo_dart

I try to connect to a MongoDB Atlas database through mongo_dart with this line of code. The provided link from MongoDB is defined by:
mongodb+srv://<user>:<PASSWORD>#test-asdf.mongodb.net/test?retryWrites=true
throws an "Invalid scheme" Error. When I cut out "+srv" and try to connect with:
Db db = new Db("mongodb://<user>:<password>#test-asdf.mongodb.net/test?retryWrites=true");
it throws a SocketException: Failed host lookup.
Is it even possible to access to a atlas mongoDB or am I forgetting something?
The mongodb+srv:// protocol is for new driver, maybe you can try to click the button "I am using driver 3.4 or earlier" to get the legacy url with mongodb:// protocol
In order to connect to atlas, you need to pass connection string which connect your atlas to mongo_dart like this:
import "package:mongo_dart/mongo_dart.dart;
void getConnection() async {
String connectionString = "mongodb+srv://<user>:<password>#test-asdf.mongodb.net/test?retryWrites=true&w=majority";
print(connectionString);
// Connect to database:
Db db = await Db.create(connectionString);
await db.open();
print(db);
}

Connection to mongoose causes ECONNREFUSE [duplicate]

I've got a problem to connect my socketIO application (made with nodeJS) with my mongoDB.
I try to connect on an remote server, but its throws me error
Here's my code (there's no user/password set in the mongoDB) :
var url = "mongodb://192.168.1.5:27017/DB"
MongoClient.connect(url, function(err, db) {
console.log("test")
if (!err) {
console.log("test");
}
else {
console.dir(err)
throw err
}
// db.close();
});
And here's when I launch the server and I tried to launch the app in a navigator :
Server listening at port 80:
{ [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }
/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error
at Error.MongoError (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:13:17)
at Server.destroy (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:629:47)
at Server.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:344:17)
at Db.close (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:267:19)
at /root/fys-realtime/examples/chat/node_modules/mongodb/lib/db.js:196:12
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/lib/server.js:226:9)
at g (events.js:180:16)
at emit (events.js:98:17)
at null.<anonymous> (/root/fys-realtime/examples/chat/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:238:68)
at g (events.js:180:16)
This error is returned for several errors like :
server is not running
you need to authenticate user
this database does not exists
the mongodb port is not the default port
Check this. Normaly your problem is just one of these causes
For this to work you have to make the changes in /etc/mongod.conf
comment bind_ip=127.0.0.1 As if this line in not commented it Listen to local interface only.
One of the solutions is to change 127.0.0.1 to public ip or whatever the router provided you with in mongodb config file located in /etc
There could be few reasons for ECONNREFUSED error.
Checkpoints
Check if your port is not serving any other process.
Check if your mongod is running.
Check if you localHost is configured correctly with your id or not.
for more you can see this

nodejs chat example does not work

I've come across a node chat example on github, When I try to run it, I see the following error:
Error connecting to mongo perhaps it isn't running ?
I've installed mongo 0.9.2, nodejs 5.2 pre, npm 3.0 and other dependencies. The example can be found here: https://github.com/gregstewart/chat.io
I can not determine whether if the example not really works or I didn't run it right. Please help.
Did you install and start mongo-db on your system? This error is mostly because of a missing mongo instance running on the local machine.
Check out the follwing code excerpts from chat.io.
main.js:
/**
* Configure the user provider (mongodB connection for user data storage)
*/
var userProvider = new UserProvider('localhost', 27017);
Creates a new UserProvider object using host and port for database (localhost:27017, mongo-db default).
UserProvider.js:
UserProvider = function(host, port) {
this.db = new mongo.Db('node-mongo-chat', new Server(host, port, {auto_reconnect: true}, {}));
this.db.addListener('error', function(error) {
console.log('Error connecting to mongo -- perhaps it isn\'t running?');
});
this.db.open(function() {
});
};
Opening the connection to the server, printing out an error on failure (the error you mentioned above).
Consider reading up on the mongo-db docs concerning installation and setup here