Shi*_*kur 6 mongoose express typescript
我收到 useNewUrlParser、useUnifiedTopology、useCreateIndex 的以下错误:
No overload matches this call.
Overload 1 of 3, '(uri: string, callback: CallbackWithoutResult): void', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; }' is not assignable to parameter of type 'CallbackWithoutResult'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'CallbackWithoutResult'.
Overload 2 of 3, '(uri: string, options?: ConnectOptions | undefined): Promise<typeof import("mongoose")>', gave the following error.
Argument of type '{ useNewUrlParser: boolean; useUnifiedTopology: boolean; useCreateIndex: boolean; }' is not assignable to parameter of type 'ConnectOptions'.
Object literal may only specify known properties, and 'useNewUrlParser' does not exist in type 'ConnectOptions'.
Run Code Online (Sandbox Code Playgroud)
索引.ts
import express from 'express'
import { errorHandler } from './middleware/error-handler';
import router from './router'
import mongoose from 'mongoose'
const app = express()
app.use(express.json())
app.use('/api/users', router)
app.use(errorHandler)
const start = async () => {
try {
await mongoose.connect("mongodb://auth-mongo-srv:27017/auth", {
useNewUrlParser: true, // error here
useUnifiedTopology: true,
useCreateIndex: true,
});
console.log("Connected to MongoDb");
} catch (err) {
console.error(err);
}
};
const PORT = 3000
app.listen(PORT, () => {
console.log("working on port ",PORT)
})
start()
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Run Code Online (Sandbox Code Playgroud)
我在 package.json 中安装了 mongoose 6.02,如果有任何帮助,我将从 dockerhub 获取 mongo 并在集群 pod 中运行。
任何帮助或建议表示赞赏。
只需删除这些选项即可,不再需要它们
https://mongoosejs.com/docs/migration_to_6.html#the-aspromise-method-for-connections
useNewUrlParser、useUnifiedTopology、useFindAndModify和useCreateIndex不再受支持的选项。Mongoose 6 的行为始终表现为useNewUrlParser、useUnifiedTopology、 和useCreateIndex为 true,而useFindAndModify为 false。请从您的代码中删除这些选项。
6.0.2版本的mongoose的ConnectOptions不包括useNewUrlParser、useUnifiedTopology、useCreateIndex。但是,6.0.2 的 mongoose 文档仍然显示这些选项有效,如果没有这些选项,mongoose 将引发拓扑错误。
将 mongoose 降级到版本 5.13.8,现在它可以正常工作。
| 归档时间: |
|
| 查看次数: |
10009 次 |
| 最近记录: |