MongoNetworkError:与本地主机的连接4:27017超时

时间:2019-04-15 06:25:50

标签: node.js mongodb

当我在本地计算机上部署应用程序时,服务器中出现连接超时错误,但一切都按预期进行,但仅对于一些较大的数据库查询,我收到此错误:

{ result:
   { result:
      { MongoNetworkError: connection 4 to localhost:27017 timed out
          at Socket.<anonymous> (/caching/node_modules/mongodb-core/lib/connection/connection.js:259:7)
          at Object.onceWrapper (events.js:277:13)
          at Socket.emit (events.js:189:13)
          at Socket._onTimeout (net.js:443:8)
          at ontimeout (timers.js:436:11)
          at tryOnTimeout (timers.js:300:5)
          at listOnTimeout (timers.js:263:5)
          at Timer.processTimers (timers.js:223:10)
        name: 'MongoNetworkError',
        errorLabels: [Array],
        [Symbol(mongoErrorContextSymbol)]: {} } } }

1 个答案:

答案 0 :(得分:0)

问题是您的查询需要很长时间才能处理。并且mongodb具有配置的默认超时。如果查询花费的时间超过此默认值,则它将失败。根据需要将所需的超时传递给连接参数。

const mongoose = require('mongoose');
const option = {
    socketTimeoutMS: 30000,
    keepAlive: true,
    reconnectTries: 30000
};

mongoose.connect(mongoURI, option);