gpt4 book ai didi

node.js - 离线时 Mongoose 与本地主机的连接失败

转载 作者:太空宇宙 更新时间:2023-11-03 23:06:38 26 4
gpt4 key购买 nike

我在使用 mongoose 连接到 Mongodb 时遇到问题。
问题是当我离线时它显示连接失败到本地主机:27017但当我在线时,它连接成功。
引用代码如下:

mongoose.connect('mongodb://localhost/database');
var db = mongoose.connection;
db.on('error', function (err) {
console.log('connection error', err);
});
db.once('open', function () {
console.log('connected.');
});

这是两种情况的屏幕截图。 first command fails when there was no internet and second one connects successfully

我是否需要在系统中进行一些配置才能使其正常工作,或者它就这样工作。

最佳答案

这是 Mongoose Can't Connect Without Internet 的重复项

无论如何,只需将连接字符串中的 localhost 替换为 127.0.0.1 即可。问题出在 Windows 内,请参阅 nodejs cannot resolve 'localhost' on windows

关于node.js - 离线时 Mongoose 与本地主机的连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33169244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com