关于mongoose连接mongodb重复访问报错的解决办法
具体代码如下所示:
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.
应该是重复打开连接失败报错,所以需要关闭连接,我加了一段关闭连接的代码,结果查询结果就成了:查询到的数据为:unde
那么该如何处理mongodb数据库连接?
解决办法如下所示:
新建一个mongoose.js:
var mongoose = require(‘mongoose'); mongoose.connect(‘mongodb://localhost/nuaidibi'); module.exports = mongoose;
每个module中,引用
var mongoose = require('./mongoose.js');下面使用就一样了,不用开关连接。
(资源库 www.zyku.net)
上一篇:CentOS中使用Yum把MySQL平滑迁移到MariaDB
栏 目:MariaDB
下一篇:关于MariaDB安装问题小记(CMake Error at)
本文标题:关于mongoose连接mongodb重复访问报错的解决办法
本文地址:https://www.zyku.net/mariadb/606.html
您可能感兴趣的文章
- 04-23ckeditor Error code: exportpdf-no-token-url. 异常
- 03-22FileZilla FTP客户端连接阿里云虚拟主机报错421 There
- 03-19MySQL 8.0创建用户授予权限报错解决方法
- 11-02mysql命令gruop by报错this is incompatible with sql
- 09-12ckfinder_php_3.4.4 IIS 报错 无效请求 (Invalid requ
- 05-08VMware创建虚拟机装系统时报错 unsuccessful 解决方法
- 03-28安装VtigerCRM报错:MySQL Server should be configured
- 03-04Composer安装报错PHP Warning: Module 'mysqli' alrea
- 04-24ThinkPHP将APP_DEBUG常量设置为false后报错的问题
- 12-11ThinkPHP 5.0 报错 A non well formed numeric value
- 11-23华为音响小艺连接wifi方法介绍
- 08-15小米平板5如何启用指纹解锁
- 12-09iqoo8pro在哪里设置经典桌面
- 04-10华为mate40e设置字体大小方法
- 01-11chic复古相机-chic复古相机应用软件功
- 02-22DedeCMS复制文章内容自动加版权代码
- 01-11光速清理管家-光速清理管家应用软件功
- 07-15帝国CMS仿站常用函数介绍
- 10-11realmegtneo2设置壁纸教程分享
- 08-13抖音在主页展示粉丝群步骤技巧
最近更新
阅读排行
猜你喜欢
- 01-24红米k30设置息屏显示时间功能方法
- 10-26荣耀50se怎么设置通知亮屏
- 12-28时分计步-时分计步应用软件功能介绍
- 02-18用ajax传递json到前台中文出现问号乱
- 01-12小仙女助手-小仙女助手应用软件功能介
- 12-25今日头条头条号注销账号教程
- 02-20解决Apache日志文件ACCESS.LOG日益膨
- 11-25小米11pro怎么恢复桌面天气组件
- 10-20小米云服务会员自动续费取消步骤分享
- 01-13亿起阅读-亿起阅读应用软件功能介绍