Tracing the documentation here and failing to load bson apparently. After running npm start I receive:
Snippet:
var mongo = require("mongodb").MongoClient;
//connect to db server
mongo.connect("mongodb://localhost:28017/myDb", function(err, db){
if(!err) {
console.log("Connected to Database")
}
else{
console.log("failed to connect");
}
});
I have tried updating/reinstalling the driver modules as well. Totally new to the framework & db and this type of error feels so trivial that it is discouraging that I am unable to figure it out. Help!
The default port for mongodb is 27017 (and then 28017 is for a web status page).
http://docs.mongodb.org/manual/reference/default-mongodb-port/
Try this connect string:
"mongodb://localhost:27017/myDb"
Related
As i'm new to api creation in nodejs and i tried to connect mongo db throung nodejs code and i have advance version of mongodb(V4) and nodejs(v10) and i could not able to connect please can any one help me.
my mongodb node code:
// Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/exampleDb", function(err, db) {
if(!err) {
console.log("We are connected");
}
});
and cmd:
commend for db connection in cmd prompt is mongod -- for connection establishement.
I've added mLab to my Heroku app, I also use mongoose. I tried use connection string from localhost, and it was working(almost). In my server file I use:
var db = mongoose.connection;
if (process.env.MONGODB_URI) {
mongoose.connect('mongodb://heroku_fb82r7lw:bbgj8uliam1psdda88fleu55li#ds161580.mlab.com:61580/heroku_fb82r7lw');
// mongoose.connect(process.env.MONGODB_URI);
} else {
mongoose.connect('mongodb://heroku_fb82r7lw:bbgj8uliam1psdda88fleu55li#ds161580.mlab.com:61580/heroku_fb82r7lw');
// mongoose.connect('mongodb://localhost/fitMe')
}
If I open the app from localhost, it saves things to the db, and can get it back, although not everything, but on heroku it doesn't work at all. I use react with server. I think that something wrong with the routs.. so here is the link to server file :
https://github.com/HelenaVolskaia/Motivation/blob/master/server/app.js
You can set an env variable locally and only use this:
// Connect Mongo
mongoose.Promise = global.Promise; // mongoose promises deprecated, use node - mongoosejs.com/docs/promises
mongoose.connect(config.db.MONGODB_URI);
mongoose.connection.once('open', () => { console.log('MongoDB Connected'); });
mongoose.connection.on('error', (err) => { console.log('MongoDB connection error: ', err); });
But regardless, add the on connection error handler and see what the error is, so you can dig deeper into why it's not connecting.
I'm trying to connect to MongoDB using Mongoose on an Amazon EC2 Linux server.
Here's my code:
var mongoose = require('mongoose');
console.log("Attempting antyhing to do with mongoose"); //shown
var db = mongoose.connection;
db.on('error',console.error.bind(console,'db connection error:')); //not shown
db.once('open',function(){
console.log("Successful connection to db!"); //not shown
});
mongoose.connect('mongodb://localhost:27017/local',function(err){
console.log("some kinda connection made"); //not shown
if(err)
{
console.log("err: "+err);
}
});
Frustratingly, I'm not getting any errors from mongoose whatsoever, but nothing seems to show up.
There seem to be a lot of questions about no callback with mongoose and mongo.
Here's a couple that I've looked at that I don't think are the problem for me:
Listen for the callback quickly:
Mongoose Connection I
moved my db.on('open'... call to before my connect call in case of a
race condition.
Is Mongo running?
Mongoose connect method fails on simple Node Server. Express, Mongoose, Path
Yes, and on port 27017
Also for reference I'm following this tutorial: https://scotch.io/tutorials/build-a-restful-api-using-node-and-express-4
One thing I am doing that I'm worried about is I've split my code up into multiple files. So this mongoose connection code is being called from a app/models/host.js (or bear.js in tutorial) file. Let me know if posting the other files would be helpful.
I also faced the same issue.
Check that the Mongoose version you are using supports the MongoDb server version
Check compatibility on this link: http://mongoosejs.com/docs/compatibility.html
Change the version of Mongoose in package.json file accordingly.
Hope this helps!
Haven't really solved the problem but I found a work-around... not using mongoose. Would still appreciate connecting to mongoose, especially as I was trying to follow a tutorial.
Here's my code that successfully connects to mongodb:
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = "mongodb://localhost:27017/host";
//Go Ahead and connect & sketchily initialize the db
var db;
var collection;
MongoClient.connect(url,function(err,database){
if(err){
console.log("Coudln't connect to mongo. Error"+err);
} else{
db = database;
collection = db.collection('hosts');
console.log("Connected to mongo, db good to go");
}
});
I've searched the Internet but couldn't find an answer for my problem. It seems specific to my application, but hopefully not :)
This error occurs both during heroku deployment as well as during dokku deployment.
But to the point.
I have the app.js file which part responsible for db connection looks like this:
/**
* DB connection
*/
var dbUrl;
if(app.get('env') === 'development'){
dbUrl = process.env.MONGOLAB_URI || "mongodb://localhost:27017/test";;
}
var db = require('./database');
db.connect(dbUrl, function(err){
if(err) {
console.log('Unable to connect to MongoDB');
} else {
console.log('MongoDB connected successfully!');
}
});
I'm working on development environment only, so I've removed the production bits.
As you can see, I'm getting dbUrl from heroku environment variables, then I'm requiring db index file which looks like this:
var MongoClient = require('mongodb').MongoClient;
var state = {
db: null
};
exports.connect = function(url, done) {
if(state.db) return done();
console.log("trying to connect to mongodb");
console.log(url);
MongoClient.connect(url, function(err, db){
console.log("mongodb callback");
if(err) return done(err);
state.db = db;
done();
});
};
And than I'm invoking the db.connect method in the app.js file (first code snippet) from the database index file (just above).
And I'm getting this error:
Starting process with command NODE_ENV=development node
server/bin/www
mongodb://user:password#ds011111.mongolab.com:33333/db_name
trying to connect to mongodb <<---- this is console.log from my db.connect method
/app/server/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
State changed from starting to crashed
The error occurs only during heroku or dokku deployment.
My db connection is 100% correct (all the environment variables are ok as well so please don't ask about it), checked it in shell, as well as connecting to it from my localhost within the app.
Basically it works on my localhost, I've deployed it on vps with ubuntu, end it works as well. The problems occurs only when I'm trying to use automation tools.
Any help greatly appreciated
Thanks!
I'm totally new with all the technologies I'm trying to do this with, but I have what seems like some simple code (gleaned from a tutorial) that I just can't get to work. I'm using Node, Express and Mongoskin/MongoDB. Whenever I try any operation against the db, I get a very generic "connection closed" error. I've got MongoDB 2.4.6, Mongoskin 0.6.0 and Mongo Native 1.3.19. MongoDB is running and I can connect from the terminal and work with my db. I see in the Mongo logging that my code never even establishes a connection. I thought maybe I need to call open explicitly, but even that returns the same error.
I'm sure I'm doing something dumb, but I'm stumped and help would be appreciated. Here's the code:
var express = require("express");
var mongoskin = require("mongoskin");
var db = mongoskin.db("localhost:28017/test", { safe: true, auto_reconnect: true });
var app = express();
app.get("/", function(request, response){
db.collection('testResult').find(function(error, result){
if (error) {
response.send("Find failed: " + error);
}
else {
response.send("got it ");
}
});
});
app.listen(8888);
Yep. I was doing something dumb. Just in case this is helpful for any other noob... The http client runs on port 28017 but MongoDB itself is actually listening on port 27017. Note the "7" in the second position. Duh. The right connection parameter (in my case), then, would be "localhost:27017/test".