0

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

QWERTY
  • 263
  • 2
  • 6
  • 21
  • seems like `db.candidates` is undefined – u.k Jun 03 '15 at 04:49
  • Also, `var db = ('candidates', ['candidates']);` sets the value to be `['candidates']` which might not be what you expect. – u.k Jun 03 '15 at 04:50
  • ah okay. . What should I need to do to fix it? – QWERTY Jun 03 '15 at 06:09
  • I don't know what you expect `db` to be, but what you code sets is an array of one string. I am guessing you want `db` to be some sort of a driver, and you are missing a function or a require call where you define it. – u.k Jun 03 '15 at 06:14

0 Answers0