Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 为什么我所有的猫鼬请求都超时了?_Node.js_Mongodb_Mongoose_Dependencies_Timeout - Fatal编程技术网

Node.js 为什么我所有的猫鼬请求都超时了?

Node.js 为什么我所有的猫鼬请求都超时了?,node.js,mongodb,mongoose,dependencies,timeout,Node.js,Mongodb,Mongoose,Dependencies,Timeout,我的猫鼬请求从昨天起就一直超时了 我的互联网连接正常,和往常一样,我的源代码没有改变 所以,我认为这一定是我的依赖关系或MongoDB本身的问题 最小可重复示例: const mongoose = require('mongoose') const mongoURL = //replace this comment with your own Mongo URL mongoose.connect(mongoURL, { useNewUrlParser: true, useUni

我的猫鼬请求从昨天起就一直超时了

我的互联网连接正常,和往常一样,我的源代码没有改变

所以,我认为这一定是我的依赖关系或MongoDB本身的问题

最小可重复示例:

const mongoose = require('mongoose')


const mongoURL = //replace this comment with your own Mongo URL

mongoose.connect(mongoURL, { 
  useNewUrlParser: true, 
  useUnifiedTopology: true, 
  useFindAndModify: false, 
  useCreateIndex: true 
})

const exampleSchema = new mongoose.Schema({
  title: String,
  author: String
})

const Example = mongoose.model('Example', exampleSchema)

const exampleOne = new Example({
  title: 'Don Quixote',
  author: 'M. Cervantes'
})

exampleOne.save().then(res => console.log(res))

mongoose.connection.close()
(node:18284) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18284) UnhandledPromiseRejectionWarning: MongooseError: Operation `examples.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (G:\Programming\Courses\Fullstack-Helsinki-2020\mongo_testing\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(node:18284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:18284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18284) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
"mongoose": {
      "version": "5.11.16",
      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.16.tgz",
      "integrity": "sha512-qmolyGAskPuq0Xr3j2Tjm9jwRccGGnLRWtTuyRvYBZoyItajwIoQdetJH8oVzs3N7aZK/GKZ82xV/t97suF8Pg==",
      "requires": {
        "@types/mongodb": "^3.5.27",
        "bson": "^1.1.4",
        "kareem": "2.3.2",
        "mongodb": "3.6.4",
        "mongoose-legacy-pluralize": "1.0.2",
        "mpath": "0.8.3",
        "mquery": "3.2.4",
        "ms": "2.1.2",
        "regexp-clone": "1.0.0",
        "safe-buffer": "5.2.1",
        "sift": "7.0.1",
        "sliced": "1.0.1"
      },
      "dependencies": {
        "mongodb": {
          "version": "3.6.4",
          "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz",
          "integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==",
          "requires": {
            "bl": "^2.2.1",
            "bson": "^1.1.4",
            "denque": "^1.4.1",
            "require_optional": "^1.0.1",
            "safe-buffer": "^5.1.2",
            "saslprep": "^1.0.0"
          }
        },
        "safe-buffer": {
          "version": "5.2.1",
          "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
          "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
        }
      }
    
运行上述示例的完整错误跟踪:

const mongoose = require('mongoose')


const mongoURL = //replace this comment with your own Mongo URL

mongoose.connect(mongoURL, { 
  useNewUrlParser: true, 
  useUnifiedTopology: true, 
  useFindAndModify: false, 
  useCreateIndex: true 
})

const exampleSchema = new mongoose.Schema({
  title: String,
  author: String
})

const Example = mongoose.model('Example', exampleSchema)

const exampleOne = new Example({
  title: 'Don Quixote',
  author: 'M. Cervantes'
})

exampleOne.save().then(res => console.log(res))

mongoose.connection.close()
(node:18284) Warning: Accessing non-existent property 'MongoError' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:18284) UnhandledPromiseRejectionWarning: MongooseError: Operation `examples.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (G:\Programming\Courses\Fullstack-Helsinki-2020\mongo_testing\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:185:20)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7)
(node:18284) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:18284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18284) DeprecationWarning: Listening to events on the Db class has been deprecated and will be removed in the next major version.
"mongoose": {
      "version": "5.11.16",
      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.16.tgz",
      "integrity": "sha512-qmolyGAskPuq0Xr3j2Tjm9jwRccGGnLRWtTuyRvYBZoyItajwIoQdetJH8oVzs3N7aZK/GKZ82xV/t97suF8Pg==",
      "requires": {
        "@types/mongodb": "^3.5.27",
        "bson": "^1.1.4",
        "kareem": "2.3.2",
        "mongodb": "3.6.4",
        "mongoose-legacy-pluralize": "1.0.2",
        "mpath": "0.8.3",
        "mquery": "3.2.4",
        "ms": "2.1.2",
        "regexp-clone": "1.0.0",
        "safe-buffer": "5.2.1",
        "sift": "7.0.1",
        "sliced": "1.0.1"
      },
      "dependencies": {
        "mongodb": {
          "version": "3.6.4",
          "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.4.tgz",
          "integrity": "sha512-Y+Ki9iXE9jI+n9bVtbTOOdK0B95d6wVGSucwtBkvQ+HIvVdTCfpVRp01FDC24uhC/Q2WXQ8Lpq3/zwtB5Op9Qw==",
          "requires": {
            "bl": "^2.2.1",
            "bson": "^1.1.4",
            "denque": "^1.4.1",
            "require_optional": "^1.0.1",
            "safe-buffer": "^5.1.2",
            "saslprep": "^1.0.0"
          }
        },
        "safe-buffer": {
          "version": "5.2.1",
          "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
          "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
        }
      }
    

问题:为什么上面的示例会引发上述错误,通常来说,为什么我的Mongoose请求都超时了?

首先,您需要等待建立连接以确保连接正常,请参阅:

其次,您需要在
save
之后调用
mongoose.connection.close()

exampleOne.save().then(res => {
  console.log(res)
  mongoose.connection.close()
})
因为您没有使用
wait
save
调用没有等待解析,因此立即调用了
mongoose.connection.close()

const res = await exampleOne.save()
console.log(res)
mongoose.connection.close()
})

首先,您需要等待建立连接以确保连接正常,请参阅:

其次,您需要在
save
之后调用
mongoose.connection.close()

exampleOne.save().then(res => {
  console.log(res)
  mongoose.connection.close()
})
因为您没有使用
wait
save
调用没有等待解析,因此立即调用了
mongoose.connection.close()

const res = await exampleOne.save()
console.log(res)
mongoose.connection.close()
})

正如我在评论中所说的,@Anatoly还说,在建立连接后,您应该发送请求(即save)

const mongoose=require('mongoose'))
const exampleSchema=新的mongoose.Schema({
标题:字符串,
作者:String
})
const Example=mongoose.model('Example',exampleSchema)
const mongoURL=//用您自己的Mongo URL替换此注释
mongoose.connect(mongoURL,{
useNewUrlParser:true,
useUnifiedTopology:正确,
UseFindModify:false,
useCreateIndex:true
})
.然后(()=>{
const exampleOne=新示例({
标题:《唐吉诃德》,
作者:《塞万提斯先生》
})
例如one.save().then(res=>{
console.log(res)
mongoose.connection.close()
})
})
.catch(错误=>{
//处理错误
})

正如我在评论中所说,并且@Anatoly说,在建立连接后,您应该发送请求(即保存)

const mongoose=require('mongoose'))
const exampleSchema=新的mongoose.Schema({
标题:字符串,
作者:String
})
const Example=mongoose.model('Example',exampleSchema)
const mongoURL=//用您自己的Mongo URL替换此注释
mongoose.connect(mongoURL,{
useNewUrlParser:true,
useUnifiedTopology:正确,
UseFindModify:false,
useCreateIndex:true
})
.然后(()=>{
const exampleOne=新示例({
标题:《唐吉诃德》,
作者:《塞万提斯先生》
})
例如one.save().then(res=>{
console.log(res)
mongoose.connection.close()
})
})
.catch(错误=>{
//处理错误
})

为什么不等待建立连接?添加
。然后在
猫鼬之后添加
。捕获
。连接
。在
中发送请求。然后
。我刚刚编辑了我的代码并尝试了,它会给我同样的错误。如果你发布了一个有效的解决方案,我会接受它作为答案。你为什么不等待建立连接呢?添加
。然后在
猫鼬之后添加
。捕获
。连接
。在
中发送请求。然后
。我刚刚编辑了我的代码并尝试了,它会给我同样的错误。如果你发布一个有效的解决方案,我会接受它作为答案。谢谢你的帮助。另外,请查看您是否可以帮助解决此相关问题:谢谢您的帮助。另外,请查看您是否可以帮助解决此相关问题: