热线电话:13121318867

登录
2020-11-09 阅读量: 783
在用 node 的 mongoose 模块操作 mongodb 数据库的时候,本来正常返回数据,但是有时候会返回空白页,控制台里报错:Topology was destroyed

问题详述:

在用 node 的 mongoose 模块操作 mongodb 数据库的时候,本来正常返回数据,但是有时候会返回空白页,控制台里报错:

Topology was destroyed

我的数据库连接代码如下:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var db = mongoose.connection;

解答:

方法如下:

先定义一个 options 对象,如下:

var options = {  
server: {
auto_reconnect: true,
poolSize: 10
}
};

poolSize是连接池最多可连接的数量,auto_reconnect是自动重新连接,设置为 true, 然后在连接数据库的时候将 options 对象当作参数传入:


mongoose.connect('mongodb://localhost/test',options);

这样就好了,问题解决!


48.5686
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子
0条评论