Mongoose 缓冲超时,当模型位于引用的项目中。

huangapple go评论70阅读模式
英文:

Mongoose buffering timed out when model is in a referenced project

问题

The project:

我创建了一个ShoppingCard项目来测试TypeScript引用。

The problem:

当模型直接包含在主项目中时,MongoDB连接成功工作。

在http://localhost:8080/api-docs/上进行了测试。

当我将模型移动到引用的项目中时,出现以下错误:

MongooseError: Operation amounttypes.find() buffering timed out after 10000ms

在https://github.com/pzoli/shoppingcard-api/tree/ref-models

https://github.com/pzoli/shoppingcard-models

shoppingcard-api中的tsconfig.json是:

{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "./build",
"strict": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"references": [{ "path": "../shoppingcard-models" }],
}

shoppingcard-models中的tsconfig.json是:

{
"compilerOptions": {
"sourceMap": true,
"target": "esnext",
"module": "CommonJS",
"outDir": "./dist",
"baseUrl": "./src",
"alwaysStrict": true,
"noImplicitAny": true,
"importHelpers": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"strictPropertyInitialization": false,
"skipLibCheck": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"types": [
"node",
],
"rootDir": "./src",
"typeRoots": [
"node_modules/@types"
],
"composite": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"dist"
]
}

shoppingcard-api中的package.json是:

{
"name": "quizserver",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node build/app.js",
"build": "tsc",
"dev": "nodemon"
},
"nodemonConfig": {
"watch": [
"src"
],
"ext": "ts",
"exec": "ts-node src/app.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.3",
"express": "^4.18.2",
"joi": "^17.9.1",
"mongoose": "^7.0.4",
"swagger-ui-express": "^4.6.2",
"typescript": "^5.0.4",
"yaml": "^2.2.1"
},
"devDependencies": {
"@types/express": "^4.17.17",
"@types/joi": "^17.2.3",
"@types/node": "^18.15.12",
"@types/swagger-ui-express": "^4.1.3",
"nodemon": "^2.0.22",
"ts-node": "^10.9.1"
}
}

shoppingcard-models中的package.json是:

{
"name": "backend-models",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"build": "tsc"
},
"author": "",
"license": "ISC",
"dependencies": {
"joi": "^17.9.2",
"mongoose": "^7.2.2",
"tslib": "^2.5.3"
}
}

请帮助找出为什么Mongoose不起作用。

英文:

The project:

I made a ShoppingCard project for testing typescript references.

The problem:

When models directly included in main project, MongoDB connection successfully works.

https://github.com/pzoli/shoppingcard-api

Tested in http://localhost:8080/api-docs/

When I move models out to referenced project I get folowing error:

MongooseError: Operation amounttypes.find() buffering timed out after 10000ms

https://github.com/pzoli/shoppingcard-api/tree/ref-models

https://github.com/pzoli/shoppingcard-models

The tsconfig.json in shoppingcard-api is:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "./build",
    "strict": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "references": [{ "path": "../shoppingcard-models" }],
}

The tsconfig.json in shoppingcard-models is:

{
  "compilerOptions": {
    "sourceMap": true,
    "target": "esnext",
    "module": "CommonJS",
    "outDir": "./dist",
    "baseUrl": "./src",
    "alwaysStrict": true,
    "noImplicitAny": true,
    "importHelpers": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "strictPropertyInitialization": false,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "types": [
      "node",
    ],
    "rootDir": "./src",
    "typeRoots": [
      "node_modules/@types"
    ],
    "composite": true
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "dist"
  ]
}

The package.json in shoppingcard-api is:

{
  "name": "quizserver",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node build/app.js",
    "build": "tsc",
    "dev": "nodemon"
  },
  "nodemonConfig": {
    "watch": [
      "src"
    ],
    "ext": "ts",
    "exec": "ts-node src/app.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "joi": "^17.9.1",
    "mongoose": "^7.0.4",
    "swagger-ui-express": "^4.6.2",
    "typescript": "^5.0.4",
    "yaml": "^2.2.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.17",
    "@types/joi": "^17.2.3",
    "@types/node": "^18.15.12",
    "@types/swagger-ui-express": "^4.1.3",
    "nodemon": "^2.0.22",
    "ts-node": "^10.9.1"
  }
}

The package.json in shoppingcard-models is:

{
  "name": "backend-models",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "tsc"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "joi": "^17.9.2",
    "mongoose": "^7.2.2",
    "tslib": "^2.5.3"
  }
}

Please help find why Mongoose not work.

答案1

得分: 0

我在mongoose的GitHub问题跟踪器中得到以下答案:

> 我猜你的独立项目有一个不同的Mongoose模块副本,所以导出model()导出的是连接到错误的Mongoose模块副本的模型。
>
> 让shoppingcard-models导出模式而不是模型,然后在你的主项目中调用model()。

英文:

I get following answer in mongoose github issue tracker:

> I'm guessing your separate project has a different copy of the
> Mongoose module, so exporting model() exports a model that's connected
> to the wrong copy of the Mongoose module.
>
> Make shoppingcard-models export schemas instead of models, and instead
> call model() in your main project.

huangapple
  • 本文由 发表于 2023年6月6日 16:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定