I cannot connect to MongoDB.
This is my code:
var MongoClient = require('mongodb').MongoClient,format =require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017', function(err, db){
if(err){
throw err;
}else{
console.log("daniel is connected");
}
})
I get in the console log that I am connected when I command 'npm start', however when I go to my URL 'http://localhost:3000/api' which is the URL I set for the DB, I get an error page displaying 'localhost refused to connect.'.
Related
i get the error when i use the mongodb atlas but when using the mongodb locally(mongo installed on my computer) the code runs very well with no errors.
http.listen(3000, function(){
console.log("Server started");
mongoClient.connect("mongodb+srv://<username>:<password>#myname.anjzd.mongodb.net/<dbname>?retryWrites=true&w=majority", function(error, client){
var database = client.db("mydatabase");
console.log("Database connected.");
});
That's because you've got an error from this callback and therefore your client will be set to null.
I guess the error comes from your URL that you put inside connect.
<username>, <password> and <dbname> are placeholders for your real username, password and database name respectively.
say your real username, password and database are ABC, 123 and myDB respectively, then your connection URL would be something like this:
let url = "mongodb+srv://ABC:123#myname.anjzd.mongodb.net/myDB?retryWrites=true&w=majority"
If the error is caused by something else you can handle it properly to detect the reason.
mongoClient.connect(url, function(error, client){
if (error) return console.log(error);
var database = client.db("mydatabase");
console.log("Database connected.");
});
I don't have much knowledge about mongo and NodeJS, and also about server set up so I might be asking something basic.
I'm trying to install MongoDB and NodeJS for school project.
In order to do so I registered to Amazon EC2 server and implemented an instance according to some tutorials I've found on line.
Using MongoDB wiki, I've installed Mongo on the server and using another Tutorial I installed NodeJS.
Just to be super clear, please see below picture who's showing the directory and MongoDB server listening:
http://s23.postimg.org/k02f747m2/mongo_Dir.jpg
After I've done that I'm trying to start using the server, I've created a simple file using the following code:
var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {});
var client = new mongodb.Db('exampledb', server, {w: 1});
client.open(function(err) {
if (err) throw err;
client.collection('students', function(err, collection) {
if (err) throw err;
collection.insert(
{
"name": "Noam",
"year": "2012."
},
{safe: true},
function(err, documents) {
if (err) throw err;
console.log('Document ID is: ' + documents[0]._id);
}
);
});
});
Now I tried to run the file and received the following error
http://s30.postimg.org/sqplfazlt/eror.jpg
Any help?
I have tried pg and other modules and packages to connect Redshift PostgreSQL through Node and Meteor.
This is my recent code written in node. It is unable to connect to Redshift. The client.connect function never responds.
But if I try to connect to some other PostgreSQL server, like localhost or some other remote server, then the code works as expected.
Same is the problem with Meteor.
var pg = require('pg');
var conString = "postgres://User:Password#EndPoint/Database";
//var conString = "postgres://postgres:postgres#localhost/postgres";
console.log("Started...");
var client = new pg.Client(conString);
console.log("Client", client);
client.connect(function(err) {
if(err) {
return console.error('could not connect to postgres', err);
}
client.query('SELECT NOW() AS "theTime"', function(err, result) {
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].theTime);
//output: Tue Jan 15 2013 19:12:47 GMT-600 (CST)
client.end();
});
});
console.log("End...");
I want to connect through Meteor. But if not possible through Meteor,
Node.js will also work.
I'm attempting to deploy a very simple app to Heroku. The code for the application can be found on Ray Wenderlich's site, here: http://www.raywenderlich.com/61078/write-simple-node-jsmongodb-web-service-ios-app I keep getting the same error whenever I try to have Heroku compile the code...
var mongoHost = "mongodb://username:password#ds041140.mongolab.com:41140/heroku_app23491233";
var mongoPort = 41140;
var collectionDriver;
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort)); //B
mongoClient.open(function(err, mongoClient) { //C
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1); //D
}
var db = mongoClient.db("heroku_app23491233"); // E
collectionDriver = new CollectionDriver(db); //F
});
When I type heroku logs, the error I get comes from if (!mongoClient) above...
app[web.1]: Error! Exiting... Must start MongoDB first
I'm sure the problem lies somewhere in my attempt to connect to the MongoLab database. I've copied the URI from MongoLab and I've created a user with the proper credentials.
I can connect to localhost just fine with very similar code, so I'm not sure what is going wrong in this example.
Thank you.
Based on the docs, my best guess is that it's because the Server constructor expects the first argument to contain only the host name (in the case of your MongoLab, ds041140.mongolab.com). However, I think you can pass your connection string into MongoClient.connect:
// Make sure to replace username and password with the proper values.
var mongoHost = "mongodb://username:password#ds041140.mongolab.com:41140/heroku_app23491233";
MongoClient.connect(mongoHost, function(err, db) {
if (err) return console.log(err);
db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) {
db.close();
if (err) return console.log(err);
console.log('Okay', result);
});
});
Documentation Page For MongoClient
Hopefully that helps!
Code:
var Db = require('mongodb').Db,
Server = require('mongodb').Server,
Client = new Db('test', new Server('127.0.0.1', 52235, {}))
In Function:
...
Client.open(function(err, pClient) {
Client.collection('test_insert', function(err, collection){
collection.find().toArray(function(err, results) {
console.log(results);
});
});
// etc.
});
This shows error: Cannot read property 'arbiterOnly' of undefined
can you help me?
This error occurs because it couldn't connect to the server and so the configuration information wasn't properly received. Probably you are using the wrong port: try 27017.