Sha*_*iri 15 mongoose mongodb typescript
我正在尝试将 Node.js 服务器连接到 Atlas mongo 数据库。我为此使用猫鼬。
\nawait mongoose\n .connect(process.env.MONGO_URI!, {\n useNewUrlParser: true,\n useUnifiedTopology: true,\n useCreateIndex: true,\n useFindAndModify: false,\n poolSize: parseInt(process.env.POOL_SIZE!),\n })\n .then((res) => {\n console.log(\n 'Connected to Distribution API Database - Initial Connection'\n );\n })\n .catch((err) => {\n console.log(\n `Initial Distribution API Database connection error occured -`,\n err\n );\n });\nRun Code Online (Sandbox Code Playgroud)\n我在 package.json 文件中与此相关的依赖项如下
\n"dependencies": {\n "@types/mongoose": "5.7.29",\n "mongoose": "5.9.21",\n "typescript": "3.9.5"\n },\nRun Code Online (Sandbox Code Playgroud)\n这是早些时候工作的,没有任何问题(我根本没有更新 @types/mongoose 或 mongoose 版本),现在突然出现以下错误
\nCompilation error in /app/src/index.ts\nUsing ts-node version 8.10.2, typescript version 3.9.5\n[ERROR] 16:25:18 \xe2\xa8\xaf Unable to compile TypeScript: src/index.ts(59,11): error TS2769: No overload matches this call.\n Overload 1 of 3, '(uris: string, callback: (err: MongoError) => void): Promise<typeof import("mongoose")>', gave the following error.\n Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; useFindAndModify: boolean; poolSize: number; }' is not assignable to parameter of type '(err: MongoError) => void'.\n Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type '(err: MongoError) => void'.\n Overload 2 of 3, '(uris: string, options?: ConnectionOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.\n Argument of type '{ useNewUrlParser: true; useUnifiedTopology: true; useCreateIndex: true; useFindAndModify: false; poolSize: number; }' is not assignable to parameter of type 'ConnectionOptions'.\n Object literal may only specify known properties, and 'poolSize' does not exist in type 'ConnectionOptions'\nRun Code Online (Sandbox Code Playgroud)\n有人可以帮我解决这个问题吗?非常感谢对此的任何想法。
\n谢谢
\n小智 27
您可以将连接功能更改为
await mongoose
.connect(process.env.MONGO_URI!, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
poolSize: parseInt(process.env.POOL_SIZE!),
} as ConnectOptions)
.then((res) => {
console.log(
'Connected to Distribution API Database - Initial Connection'
);
})
.catch((err) => {
console.log(
`Initial Distribution API Database connection error occured -`,
err
);
});
Run Code Online (Sandbox Code Playgroud)
您可以使用它来导入 ConnectOptions
import mongoose, { ConnectOptions } from "mongoose";
Run Code Online (Sandbox Code Playgroud)
小智 8
npm 包“@types/mongoose”已被弃用,因为最新版本的 Mongoose 发布了自己的类型。
https://www.npmjs.com/package/@types/mongoose
因此,最新版本的 mongoose (6.0.3) 不提供 useNewUrlParser、useCreateIndex、useUnifiedTopology 和 useFindAndModify 的连接选项。
要解决这个问题,请将 mongoose 降级到旧版本,例如5.13.8,然后它应该可以正常工作,没有任何问题。:)
对于那些使用截至本文最新版本的用户,useNewUrlParser、useUnifiedTopology、useFindAndModify和useCreateIndex 不再受支持的选项。
请参阅下面更新的示例代码:
const URI = 'mongodb://127.0.0.1:27017/test';
await mongoose.connect(URI, {
useNewUrlParser: true, // <-- no longer necessary
useUnifiedTopology: true // <-- no longer necessary
});
Run Code Online (Sandbox Code Playgroud)
自 v6 发布以来,Mongoose 的行为始终为 if useNewUrlParser、useUnifiedTopology、useCreateIndexare true、useFindAndModifyis false。