Che*_*Lee 5 mongodb express typeorm nestjs
我在自己的 Ubuntu 服务器上安装了 mongodb 软件,并得到了这样的 mongo 字符串mongodb://xxx:pass@42.212.159.109:27017,当我在本地终端中键入该字符串并使用时mongosh mongodb://xxx:pass@42.212.159.109:27017,它工作正常并且连接成功。但是当我在 Nestjs 项目中使用这个 mongodb 字符串时,当我运行时npm run start,终端输出MongoServerError: Authentication failed.
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const username = configService.get('MONGO_USER');
const password = configService.get('MONGO_PASS');
const database = configService.get('MONGO_DATABASE');
const host = configService.get('MONGO_HOST');
const port = configService.get('MONGO_PORT');
return {
type: 'mongodb',
host,
port,
username,
password,
database,
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
};
},
Run Code Online (Sandbox Code Playgroud)
mongodb版本是4.1.3,TypeOrm版本是0.2.38。有谁知道问题是什么以及如何解决?感谢你。
我已经弄清楚问题出在哪里了,因为我的mongodb的authSource与保存数据的数据库不是同一个。所以我应该将 authSource 选项设置为 admin,然后它就可以工作了。
return {
type: 'mongodb',
host,
port,
username,
password,
database,
authSource: 'admin',
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: true,
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3411 次 |
| 最近记录: |