I am very close to an express app working in a sandbox environment on Heroku with a MongoDB addon..
I am still seeing a ReferenceError in the log, which seems to be the reason for the app crashing.
The first error was regarding this configuration:
//MongoDB
var mongodb = require('mongodb');
var db;
var MONGODB_URI = process.env.MONGOLAB_URI;
var PORT = process.env.PORT;
mongodb.MongoClient.connect(MONGODB_URI, function(err, database) {
if(err) throw err;
db = database;
app.listen(PORT);
console.log('Listening on port ' + PORT);
});
var testColl = db.collection('test');
That led to the log reading a TypeError, probably because db was defined locally..
app[web.1]: var testColl = db.collection('test');
app[web.1]: TypeError: Cannot call method 'collection' of undefined
And the recent error was a ReferenceError:
app[web.1]: var db = database;
app[web.1]: ReferenceError: database is not defined
Probably because database is defined globally, but not referenced within the context of the connection..
//MongoDB
var MongoClient = require('mongodb').MongoClient,
var Server = require('mongodb').Server;
var mongoUri = process.env.MONGOLAB_URI;
var port = process.env.PORT;
var db = database;
var mongoclient = new MongoClient(new Server(mongoUri,
{ 'native_parser' : true }));
var testColl = db.collection('test');
mongoclient.open(function (err, mongoclient) {
if (err) throw err
app.listen(port);
console.log("Express server started on port " + port);
});
How can I reformat the code to launch this app on Heroku?
Your first example looks close to working, but there's one thing wrong. mongodb.connect is an asynchronous call, which means it returns right away. That's why it has the callback parameter. Because the code runs so fast, it doesn't have time to actually connect to the database before you try to use the db object, which is what causes your crash in the first set of code.
If you move your database call inside the callback, it should work.
Edit: This is in reply to your comment, but I can't include much code in another comment, so you get an edit.
It really depends on your use case, but I realize that's not very helpful. So here's one way you could do it:
module.exports = function() {
return db;
};
By doing so, you would be able to require it from another file, and use it as such:
var http = require('http');
var db = require('./db.js');
http.createServer(function(req, res) {
db().collection('test', function(err, collection) {
console.log(err, collection);
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('We got it! Unless there is an error...\n');
});
}).listen(8080);
There's definitely ways to improve upon that, but it should get you started.
Related
when I try node index.js my error comes with TypeError: 'connect' only accepts a callback, then I'm confused about how to solve this problem.
The error goes to mongo_client.js:165:11
I did a tutorial about how to connect to mongodb with Nodejs but, I found a problem that ppl never had since now
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient();
const url = 'mongodb://localhost:27107/testedmtdev';
MongoClient.connect(url, function(err, client){
if(err){
console.log('Unable to connect to the Mongodb server. Error : ', err);
} else {
app.listen(3000, ()=>{
console.log('Connected to mongodb, webservice running on port 3000');
})
}
});
I expect the output of 'Connected to mongodb, webservice running on port 3000'
It's because you're actually calling MongoClient with no parameters.
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient(); // <-- your calling the method here
Make a change to just use a reference
const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient; // <-- do *not* call method
https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html#mongoclient-connect
I am using node-run-cmd package to start the mongodb server in my app.js file. I know this works because I can see the collections on Robomongo when my script is running as well as the mongod.exe in my list of running processes. The problem is trying to connect to the db called testDB. Below is the commented code.
//start mongodb server
//this works
var nrc = require('node-run-cmd');
var cmd = '..\\mongodb\\mongod --dbpath '+__dirname+'\\db';
nrc.run(cmd);
//setup
var express = require('express');
var MongoClient = require('mongodb').MongoClient;
var app = express();
app.use(express.static('public'));
app.set('view engine', 'ejs');
//connect to mongo
//this fails to connect to db testDB
var url = 'mongodb://localhost:27017/testDB';
MongoClient.connect(url, function(err, db) {
if(!err) {
console.log("connection successful");
}
else{
console.log(err.message)
}
});
Here is the err.message
failed to connect to server [localhost:27017] on first connect
Any idea what I am doing wrong here. My assumption is that the db connection is trying before the server has fully started but I am not completely sure.
EDIT:
so that's what it was, timing issue. I tried the following and it connected to the DB. Is there a graceful way of doing this other than what I have here?
function connect(){
var url = 'mongodb://localhost:27017/testDB';
MongoClient.connect(url, function(err, db) {
if (!err) {
console.log("connection successful");
}
else {
console.log(err.message)
}
});
}
setTimeout(connect, 10000);
You should use the callback in the node_run_cmd package (https://www.npmjs.com/package/node-run-cmd#callback-style).
Place your connect function inside the callback.
You will probably also want to only start express here as well.
I am using the express framework and would like to connect to a mongodb without using mongoose, but with the native nodejs Mongodb driver. How can I do this without creating a new connection every time?
To handle get or post requests I currently open a new connection to the db for every request and close it on completion of the request. Is there a better way to do this? Thanks in advance.
Following the example from my comment, modifying it so that the app handles errors rather than failing to start the server.
var express = require('express');
var mongodb = require('mongodb');
var app = express();
var MongoClient = require('mongodb').MongoClient;
var dbURL = "mongodb://localhost:27017/integration_test";
var db;
// Initialize connection once
MongoClient.connect(dbURL, function(err, database) {
if(err) return console.error(err);
db = database;
// the Mongo driver recommends starting the server here
// because most apps *should* fail to start if they have no DB.
// If yours is the exception, move the server startup elsewhere.
});
// Reuse database object in request handlers
app.get("/", function(req, res, next) {
var collection = "replicaset_mongo_client_collection";
db.collection(collection).find({}, function(err, docs) {
if(err) return next(err);
docs.each(function(err, doc) {
if(doc) {
console.log(doc);
}
else {
res.end();
}
});
});
});
app.use(function(err, req, res){
// handle error here. For example, logging and
// returning a friendly error page
});
// Starting the app here will work, but some users
// will get errors if the db connection process is slow.
app.listen(3000);
console.log("Listening on port 3000");
var mongodb = require('mongodb');
var uri = 'mongodb://localhost:27017/dbname';
module.exports = function(callback) {
mongodb.MongoClient.connect(uri, callback);
};
Ad this snippet in a file say connect.js and then require this file(connect.js) in your file where you are declaring your functions for http requests.
While going through a mongodb tutorial, I ran into an issue with this configuration:
var MongoClient = require('mongodb').MongoClient;
var mongoClient = new MongoClient(new server('localhost', '27017', {'native_parser': true}))
var db = mongoClient.db('test');
TypeError: Object # has no method 'db'
Eventually, I was able to solve it using mongodb server
var server = require('mongodb').Server,
Db = require('mongodb').Db;
var db =new Db('test', new server('localhost', '27017', {'native_parser': true}));
db.open(function(err, res){
app.listen(8080);
console.dir('app started on 8080');
});
However, the documentation says "Server should not be used, use the MongoClient.connect."
Based on this, I'd like to know when is the appropriate time to use the server?
Here is an example on how to use it in regards to the deprecation present in 2.0 and your setup and usage of callbacks instead of promises:
var mongoDB = require('mongodb');
var theDB = null;
mongoDB
.MongoClient
.connect('mongodb://localhost:27017/test', null, function(err, db) {
if (err) {
console.error(err);
} else {
theDB = db;
app.listen(8080);
console.dir('app started on 8080');
}
});
I am using Cloude 9 environment for developing my nodejs app. In that I have written code to connect to mongodb database. I am successfully connecting to database and adding record to collection.
Now I want to send the collection info in return. But using res.send(collectionInfo); is not working.
Let me know how should I achieve this
Below is the code of my server.js file
var Db = require('mongodb').Db;
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var ejs = require('ejs');
var app = express();
var helpers = require('express-helpers')
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var db;
helpers(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded
var server = http.Server(app);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.use(express.static(__dirname + '/client'));
app.use(express.static(path.join(__dirname, '/client')));
// MongoDB Connection
app.use(function(req, res, next) {
next();
})
app.get('/monogdb', function (req, res) {
res.render('monogdb.ejs');
});
app.post('/ajax-mongo-connect', function (req, res) {
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
if(err){
console.log(err);
}else{
var db = mongoClient.db("mydb");
db.createCollection("students", { name : req.body.nm, description : req.body.desc, location : req.body.loc } );
console.log('database connected',db);
var collectionInfo = db.collection("students");
mongoClient.close();
//res.setHeader('Content-Type', 'application/json');
res.send(collectionInfo);
}
})
})
As per #Roman Sachenko answer, I have tried to use
res.send(collectionInfo.toJSON()); but it is giving below error
/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:299
throw err;
^
TypeError: Object #<Collection> has no method 'toJSON'
at /home/ubuntu/workspace/server.js:66:41
at MongoClient.open
(/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:103:5)
at Db.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:296:11)
at process._tickCallback (node.js:442:13)
and using res.send({data: collectionInfo}); gives error
home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:299
throw err;
^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at ServerResponse.res.json (/home/ubuntu/workspace/node_modules/express/lib/response.js:185:19)
at ServerResponse.res.send (/home/ubuntu/workspace/node_modules/express/lib/response.js:117:21)
at /home/ubuntu/workspace/server.js:67:21
at MongoClient.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/mongo_client.js:103:5)
at Db.open (/home/ubuntu/workspace/node_modules/mongodb/lib/mongodb/db.js:296:11)
at process._tickCallback (node.js:442:13)
Try to return this: res.status(200).json({'myCollection' : collectionInfo});.
You can find more details about express response here
Update:
After you explain the details, take a look at the code below:
app.post('/ajax-mongo-connect', function (req, res) {
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
if(err){
console.log(err);
res.status(500).json({message : 'OMG, an error occurred'});
}else{
var db = mongoClient.db("mydb");
db.createCollection("students", { name : req.body.nm, description : req.body.desc, location : req.body.loc } );
console.log('database connected',db);
var collectionInfo = db.collection("students");
// Here we will find all students
collectionInfo.find({}).toArray(function(err, students) {
// so now, we can return all students to the screen.
res.status(200).json({'myCollection' : students});
}
}
})
})
Cheers!
Mongoose ODM
First of all I would like to recommend you using Mongoose ODM:
https://github.com/Automattic/mongoose
So you will make you work with database much easier.
Basically it returns (Mongoose) normal object as results, but in case of issues you may try to use toObject() or toJSON() or as it mentioned create own object like {data: mongoCollection}
Examples:
res.send(collectionInfo.toObject());
res.send(collectionInfo.toJSON());
res.send({data: collectionInfo});
Please refer to the link in case of questions:
http://mongoosejs.com/docs/guide.html#toJSON
Native Driver
As for native driver, it also should return normally-constructed object, but according to issues I faced with in the past, JSON.stringify always helps if you set headers manually.
You may also check the contents of your entity. So you can just output it by console.log(collectionInfo);
Then just make sure that there is correct object inside.
And according to results you can take actions like:
res.send(JSON.stringify(collectionInfo)) //set headers manually
res.json(JSON.stringify(collectionInfo)) //you don't need to set headers
At least you will know what exactly is inside of collectionInfo. I think it will be enough to investigate the issue.
You can view circular JSON objects by doing this in node.js:
const util = require('util') // Native node module
util.inspect(circularObj)
You can call it from anywhere in the code, so it's very versatile.