I'm using mongoDB(mongojs) and nodejs. When I run my server.js I get the following error. TypeError: Cannot call method 'find' of undefined
I do not know why is this happen.This is my server.js
:
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server)
// var mongojs = require('mongojs');
// var db = mongojs('candidates', ['candidates']);
var databaseUrl = "http://localhost:27017/candidates";
var db = ('candidates', ['candidates']);
var mongojs = require('mongojs').connect(databaseUrl, db);
var bodyParser = require('body-parser');
app.use(express.static(__dirname + "/public"));
app.use(bodyParser.json());
io.sockets.on("connection", function(socket) {
console.log('user connected');
socket.on("disconnect", function(o) {
console.log('user left');
});
socket.emit("connected",{message:'helloworld'});
socket.on("addVote",function(vote){
console.log('adding vote');
console.log(vote);
io.sockets.emit("addVote",vote);
})
socket.on("unVote",function(vote){
console.log('unvote');
console.log(vote);
io.sockets.emit("unVote",vote);
})
socket.on("getPrecinct",function(no){
console.log('getPrecinct');
console.log(no);
io.sockets.emit("getPrecinct",no);
})
socket.on("nextBallot",function(add){
console.log('nextBallot');
console.log(add);
io.sockets.emit("nextBallot",add);
})
})
app.get('/candidates', function (req, res) {
console.log('I received a GET request');
db.candidates.find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.put('/updateVotes/:precint_id', function (req, res) {
var id = req.params.precint_id;
db.candidates.findAndModify({
query: {precint_id:id },
update: {$set: {votes: req.body}},
new: true}, function (err, doc) {
console.log(err);
if(doc == null){
db.candidates.save({precint_id:id,votes:req.body})
}
}
);
});
server.listen(3000);
console.log("Server running on port 3000");
The error happens when I add the variable databaseUrl. I add it because If I have 3 computers I want them to connect to 1 computer server with mongodb installed. Why I get the error of Cannot call method 'find' of undefined
. What am I missing? Any help. Thank you