如何在 Mongoose 与 TypeScript 中移除 "current URL string parser is deprecated" 警告
大家好!我正在用 Express 和 TypeScript 编写后端,并通过 Mongoose 使用 MongoDB。连接数据库时,我在控制台中看到以下警告:
```
(node:12345) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
```
我已经尝试在 options 中添加 `useNewUrlParser: true`,但仍然看到这条消息。这是我的代码:
```typescript
import mongoose from 'mongoose';
import { config } from './config';
const connectDB = async (): Promise => {
try {
await mongoose.connect(config.mongoURI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB connected successfully');
} catch (error) {
console.error('MongoDB connection error:', error);
process.exit(1);
}
};
export default connectDB;
```
版本信息:
- mongoose: 6.0.0
- typescript: 4.5.0
- node: 16