无法阅读财产'收集'未定义的

时间:2016-05-12 05:13:04

标签: node.js mongodb

我试图通过html表单在mongoDB中插入数据但是我一直收到以下错误:

  

无法阅读财产'收集'未定义的



var express = require('express');
var bodyParser = require('body-parser');
var app=express();
app.set('views',__dirname+'/views');
app.set('view engine','ejs'); //extension of views
app.use(bodyParser.urlencoded({extended:false}));

//mongo db setup in node
var mongo = require('mongodb');
db = new mongo.Db('mydb', new mongo.Server("127.0.0.1", 27017, {}), {});

// opening views
app.get('/',function(req, res) {
	res.render('index');
})

// submit post data
app.post('/insert', function(req, res) {
	db.open(function(err, db) {
		db.collection('students', function(err, collection) {
			doc = {
				"sno": req.body.sno,
				"name": req.body.name
			};

			collection.insert(doc, function() {
				res.send("inserted Successfully!");
				db.close();
			});
		});
	});
});

// show data
app.get('/show', function(req, res) {
	db.open(function(err, db) {
		var getStudents = function(db, callback) {
			var cursor = db.collection('students').find();
			cursor.each(function(err, doc) {
				assert.equal(err, doc);
				if(doc != null) {
					res.send(cursor);
				} else {
					callback();
				}
			});
		};
	});
});

// start Server
var server = app.listen(8081, function() {
	var host = server.address().address
	var port = server.address().port

	console.log("example app listening at http://5s:%s", host, port)
})




0 个答案:

没有答案