【发布时间】:2021-10-18 17:06:18
【问题描述】:
我正在尝试将我的 Node.js 服务器连接到 Atlas mongo 数据库。我正在为此使用猫鼬。
await mongoose
.connect(process.env.MONGO_URI!, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
poolSize: parseInt(process.env.POOL_SIZE!),
})
.then((res) => {
console.log(
'Connected to Distribution API Database - Initial Connection'
);
})
.catch((err) => {
console.log(
`Initial Distribution API Database connection error occured -`,
err
);
});
我在 package.json 文件中与此相关的依赖项如下
"dependencies": {
"@types/mongoose": "5.7.29",
"mongoose": "5.9.21",
"typescript": "3.9.5"
},
这在早期工作时没有任何问题(我根本没有更新 @types/mongoose 或 mongoose 版本),现在突然出现以下错误
Compilation error in /app/src/index.ts
Using ts-node version 8.10.2, typescript version 3.9.5
[ERROR] 16:25:18 ⨯ Unable to compile TypeScript: src/index.ts(59,11): error TS2769: No overload matches this call.
Overload 1 of 3, '(uris: string, callback: (err: MongoError) => void): Promise<typeof import("mongoose")>', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; useFindAndModify: boolean; poolSize: number; }' is not assignable to parameter of type '(err: MongoError) => void'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type '(err: MongoError) => void'.
Overload 2 of 3, '(uris: string, options?: ConnectionOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.
Argument of type '{ useNewUrlParser: true; useUnifiedTopology: true; useCreateIndex: true; useFindAndModify: false; poolSize: number; }' is not assignable to parameter of type 'ConnectionOptions'.
Object literal may only specify known properties, and 'poolSize' does not exist in type 'ConnectionOptions'
有人可以帮我吗?非常感谢您对此的任何想法。
谢谢
【问题讨论】:
-
Mongoose 有自己的类型,
@types/mongoose已弃用:npmjs.com/package/@types/mongoose。 -
@jonrsharpe 但是旧代码仍然可以正常工作吗?因为我根本没有更新到最新的。我很困惑为什么会出现这个问题
-
我怀疑这是因为你以前有选项作为变量,现在有一个对象文字。不会检查变量是否有多余的属性,而对象文字会检查:playground
-
但这工作正常。我从未更改过此代码。它总是如上所示。我可以想象如果我更新 typescript 包或我没有更新的 @types/mongoose 会发生这种情况
-
这不是新的 api 驱动程序客户端选项的一部分。仅供参考
标签: typescript mongodb mongoose