javascript - Node Mongodb TypeError: Cannot read property 'find' of undefined -
still working on rest service mongo , node , got error
it's kind of weird cause have route call users in mongo , runs out problems, difference it's i'm using 'find' fro , 'findone' ones i'm calling id.
read findone it's depreciate in mongo 4 or more can't find proper syntax it.
these package , .js i'm working with.
//app.js var express = require('express'); var app = express(); var morgan = require('morgan'); var bodyparser = require('body-parser'); var users = require('./routes/users'); //app settings //configure app jade rendering engine app.set('view engine','jade'); app.use(express.static('public')); //add morgan app.use(morgan('dev')); /* 'default','short','tiny','dev' */ app.use(bodyparser.urlencoded({extended:false})) app.use(bodyparser.json()) //root '/' request app.get('/', function(req, res) { res.render('index'); }); // services request app.get('/services', function(req, res) { res.render('services'); }); // users request app.get('/users', users.findall); app.get('/users/:id', users.findbyid); app.post('/users', users.adduser); app.put('/users/:id', users.updateuser); app.delete('/users/:id', users.deleteuser); // connectiong db mongo var mongo = require('mongodb'); var server = mongo.server, db = mongo.db, bson = mongo.bsonpure; var server = new server('localhost', 27017, {auto_reconnect: true}); db = new db('test_gwi', server); db.open(function(err, db) { if(!err) { console.log("connected 'test_gwi' database"); db.collection('users', {strict:true}, function(err, collection) { if (err) { console.log("the 'users' collection doesn't exist. creating sample data..."); populatedb(); } }); } }); //mongo querys & shit //port listening app.listen(3000); console.log('listening on port 3000...');
//users var mongo = require('mongodb'); var server = mongo.server, db = mongo.db, bson = mongo.bsonpure; var server = new server('localhost', 27017, {auto_reconnect: true}); db = new db('test_gwi', server); db.open(function(err, db) { if(!err) { console.log("connected 'test_gwi' database"); db.collection('users', {strict:true}, function(err, collection) { if (err) { console.log("the 'test_gwi' collection doesn't exist. creating sample data..."); populatedb(); } }); } }); exports.findbyid = function(req, res) { var id = req.params.id; console.log('retrieving user: ' + id); db.collection('users', function(err, collection) { collection.find({'_id':new bson.objectid(id)}, function(err, item) { res.send(item); }); }); }; exports.findall = function(req, res) { db.collection('users', function(err, collection) { collection.find().toarray(function(err, items) { res.send(items); }); }); };
package
{ "name": "testgwi", "version": "1.0.0", "description": "rest services", "main": "index.js", "scripts": { "test": "echo \"error: no test specified\" && exit 1" }, "author": "", "license": "isc", "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.4", "jade": "^1.11.0", "mongoose": "^4.11.7", "morgan": "^1.8.2" } }
Comments
Post a Comment