var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
var Person = mongoose.model('Person',{
id:Number,
name:String
});
/*新增*/
router.get('/insert', function(req, res){
var student = new Person({
id:1,
name:"huop"
});
mongoose.connect("mongodb://localhost:27017/test");
student.save(function(e, product, numberAffected) {
if (e) res.send(e.message);
var html = "
新增的数据为:" + JSON.stringify(product);
html += "
影响的数据量为:" + numberAffected;
res.send(html);
});
});
router.get('/find',function(request, response){
mongoose.connect("mongodb://localhost:27017/test");
Person.find({
id: 1
}, function(e, docs) {
if (e) response.send(e.message);
var html = "
查询到的数据为:" + JSON.stringify(docs);
response.send(html);
});
});
以上代码可以正常执行,但是有个问题,就是第一次访问的时候正常,但是刷新一遍就会报错,Trying to open unclosed connection.undefined
Error: Trying to open unclosed connection.