0

Im having major issues connecting to my mongohq data base. I am following this tutorial but replacing the local mongo DB with a remote mongoHQ db: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

wines.js

var mongo = require('mongodb'); 
Db = mongo.Db;
BSON = mongo.BSONPure;
con = null;

server = new Server('troup.mongohq.com', 'mongo_port', {auto_reconnect: true});
DBCon = new Db('dev', server, {safe: false});
DBCon.open(function(err, db) {
  if(!err) {
   db.authenticate('username', 'password', function(err){
    if(!err) con = db;
   })
  }
 })

// exports.findById = function(req, res) {
//     var id = req.params.id;
//     console.log('Retrieving wine: ' + id);
//     db.collection('wines', function(err, collection) {
//         collection.findOne({'_id':new BSON.ObjectID(id)}, function(err, item) {
//             res.send(item);
//         });
//     });
// };

server.js

var express = require('express'),
path = require('path'),
http = require('http'),
wine = require('./routes/wines');

var app = express();

app.configure(function () {
app.set('port', process.env.PORT || 3000);
app.use(express.logger('dev'));  /* 'default', 'short', 'tiny', 'dev' */
app.use(express.bodyParser()),
app.use(express.static(path.join(__dirname, 'public')));
});

app.get('/wines', wine.findAll);
app.get('/wines/:id', wine.findById);
app.post('/wines', wine.addWine);
app.put('/wines/:id', wine.updateWine);
app.delete('/wines/:id', wine.deleteWine);

http.createServer(app).listen(app.get('port'), function () {
console.log("Express server listening on port " + app.get('port'));
});

error im getting when trying to start node:

/Sites/nodecellar/routes/wines.js:31
db.open(function(err, db) {
^
ReferenceError: db is not defined
    at Object.<anonymous> (/Sites/nodecellar/routes/wines.js:31:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Sites/nodecellar/server.js:4:12)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

Any advice on what is wrong here?

Stennie
  • 63,885
  • 14
  • 149
  • 175
Anthony
  • 2,330
  • 8
  • 44
  • 64
  • 1
    Could you provide more context? I'm not seeing the `db.open` call in wines.js (although I do see a `DBCon.open`) and it's hard to gauge where the issue actually is. – George Feb 04 '14 at 16:14

1 Answers1

0

Your problem is almost certainly with the server connection setup. The tutorial predates updates to the driver that made the interface uniform across all platforms and the accepted way is to set up using the MongoClient class.

Look at the notes on the URL connection format as you likely need to pass in your credentials and database to connect to there.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317