i'm using mongodb(mongojs) , nodejs. when run server.js following error. typeerror: cannot call method 'find' of undefined
i not know why happen.this 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 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 add variable databaseurl. add because if have 3 computers want them connect 1 computer server mongodb installed. why error of cannot call method 'find' of undefined
. missing? help. thank you
Comments
Post a Comment