Error: Trying to open unclosed connection.
错误:试图打开未关闭的连接。在我自己写model文件的时候,重复打开了连接数据库的操作,所以报这种错误。
错误实例:
两个model文件:
userModel.js
var mongoose = require('mongoose');
var config = require('./../config/config');
mongoose.connect(config.mongodb);
var PostSchema = new mongoose.Schema({
title:String,//标题
author:String,//作者
article:String,//文章内容
publishTime:String,//发表时间
postImg:String,//封面
comments:[{
name:String,
time:String,
content:String
}],//评论
pv:Number//访问次数
});
module.exports = mongoose.model('Post',PostSchema );
userModel.js
var mongoose = require('mongoose');
var config = require('../cofig/config');//引用同级目录下的文件
mongoose.connect(config.mongodb);
var UserSchema = new mongoose.Schema({
username:String,
password:String,
email:String
}); module.exports = mongoose.model('User', UserSchema);
我们应该在我们运行我们的项目的时候打开数据库的连接,之后再每个model文件直接访问就可以,不要每一次都要进行连接。修正后的代码:
在我们项目的app.js或index.js(就是所谓的启动文件)增加共有的数据库连接
var mongoose = require('mongoose');
var dbName = 'blog'; //数据库的名称
var url = 'mongodb://localhost/'+dbName;
var mongoOptions = {
server: {
socketOptions: {
keepAlive: 1
}
}
};
mongoose.connect(url, mongoOptions);
mongoose.connection.on('error', function (err) {
console.log('Mongo Error:' + err);
}).on('open', function () {
console.log('Connection opened');
});
之后再每个model进行引用:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username:String,
password:String,
email:String
}); module.exports = mongoose.model('User', UserSchema);
Error: Trying to open unclosed connection.的更多相关文章
- 【总算解决了】A network-related or instance-specific error occurred while establishing a connection to SQL Server
给别人做的网站莫名其妙连接不上数据库.百度了好多,总算知道自己的错在哪了. 报 "A network-related or instance-specific error occurred ...
- SSH error ( Read from socket failed: Connection reset by peer ) and it's solution
SSH error ( Read from socket failed: Connection reset by peer ) and it's solution ssh cann't connect ...
- 6、Flutter Error waiting for a debug connection: ProcessException: adb did not report f(转)
1.错误信息 Error waiting for a debug connection: ProcessException: adb did not report forwarded port 2.解 ...
- 【SQL Server 问题记录】A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
本文涉及的相关问题,如果你的问题或需求有与下面所述相似之处,请阅读本文 A network-related or instance-specific error occurred while esta ...
- [原]openstack-kilo--issue(十)ERROR: openstack Unable to establish connection to http://controller:35357/v3/auth/tokens
====环境== openstack :kilo CentOS : 7 ====问题=== 在没有关vm的情况下,重启了controller. 问题一: 在使用nova service-list 或者 ...
- ssh-copy-id to filter out any that...ERROR: Read from socket failed: Connection reset by peer
ssh-copy-id bigboss1 /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter ...
- A network-related or instance-specific error occurred while establishing a connection to SQL Server
今天同事给我发了个图片过来, 服务器环境 sql 2000 + IIS7 看到这张图片,我先自己试了下,确实是有这个问题的,而且不是偶然性的,那么再看报错意思,在跟sql建立连接的时候发生了一个错误 ...
- cloudermanager安装时database connection出现Unexpected error. Unable to verify database connection(图文详解)
不多说,直接上干货! http://www.aboutyun.com/forum.php?mod=viewthread&tid=20455&extra=&page=2 欢迎大家 ...
- Error (10663): Verilog HDL Port Connection error at led_demo.v(6): output or inout port "led" must be connected to a structural net expression
错误现象:
随机推荐
- python之系统编程 --进程
1.调试(PDB) 代码: [root@master gaoji]# vim test2.py 1 #!/usr/local/bin/python3 2 # -*- coding:utf-8 -*- ...
- 如何卸载ubuntu软件
你的硬盘空间已经不太足够了?如果你使用的是Ubuntu操作系统,你可能想知道如何能够卸载过时.无用的程序.有几种方法可以卸载程序,包括图形化方法和命令行方法.参考本指南,采用最适合你的方法卸载程序. ...
- poj3013Big Chrismas Tree——树转换spfa
题目:http://poj.org/problem?id=3013 看似生成树,实则最短路,可以将题意转化为点权*根到此点的边权和(最短路使其最小). 代码如下: #include<iostre ...
- WPF error: does not contain a static 'Main' method suitable for an entry point
WPF error: does not contain a static 'Main' method suitable for an entry point doe ...
- Spring 源码解析之DispatcherServlet源码解析(五)
spring的整个请求流程都是围绕着DispatcherServlet进行的 类结构图 根据类的结构来说DispatcherServlet本身也是继承了HttpServlet的,所有的请求都是根据这一 ...
- POJ-3187
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7634 Accepted: 43 ...
- CF-828B
B. Black Square time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- IIS PHP的Loaded Configuration File为空解决[转]
在Windows Server 2003上,IIS配置支持PHP,发现PHP扩展未加载,phpinfo()查看,显示 Configuration File (php.ini) Path (none) ...
- JDK的windows和Linux版本之下载
简单说下,Eclipse需要Jdk,MyEclipse有自带的Jdk,除非是版本要求 http://www.oracle.com/technetwork/java/javase/downloads/i ...
- Protobuf 文件生成工具 Prototool 命令详解
Protobuf 文件生成工具 Prototool 命令详解 简介 Prototool 是 Protobuf 文件的生成工具, 目前支持go, php, java, c#, object c 五种语言 ...