[英]Unable to make mongodb connection using adonis and mongodb
I am trying to make connection to mongodb.我正在尝试连接到 mongodb。 I am using adonisJS and Mongoose.我正在使用 adonisJS 和 Mongoose。 Mongodb connection string is correct and I have created todo
database. Mongodb 连接字符串是正确的,我已经创建了todo
数据库。 But I am getting Authentication failed
error.但我收到Authentication failed
错误。
MongoServerError: Authentication failed.
at Connection.onMessage (/opt/projects/demo/adonis-demo/adonis-mongoose-project/node_modules/mongodb/src/cmap/connection.ts:438:20)
at MessageStream.<anonymous> (/opt/projects/demo/adonis-demo/adonis-mongoose-project/node_modules/mongodb/src/cmap/connection.ts:256:56)
at MessageStream.emit (events.js:376:20)
at processIncomingData (/opt/projects/demo/adonis-demo/adonis-mongoose-project/node_modules/mongodb/src/cmap/message_stream.ts:193:14)
at MessageStream._write (/opt/projects/demo/adonis-demo/adonis-mongoose-project/node_modules/mongodb/src/cmap/message_stream.ts:70:5)
at writeOrBuffer (internal/streams/writable.js:358:12)
at MessageStream.Writable.write (internal/streams/writable.js:303:10)
at Socket.ondata (internal/streams/readable.js:745:22)
at Socket.emit (events.js:376:20)
at addChunk (internal/streams/readable.js:309:12) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed',
[Symbol(errorLabels)]: Set(1) { 'HandshakeError' }
}
My Controller as below,我的 Controller 如下,
'use strict'
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
import mongoose, { Schema } from 'mongoose'
const MONGOURI ='mongodb://root:1234@localhost:27017/todo';
export default class TasksController {
constructor() {
(async ()=>{
await this.connection();
})();
}
public async connection() {
try {
await mongoose.connect(MONGOURI);
mongoose.connection.once('open', (abc) => {
console.log('Server running on port ',abc);
});
mongoose.connection.on('error', error => console.error(error));
} catch (error) {
console.log("Catch Error: ",error);
}
}
public async index ({ }: HttpContextContract) {
const Task = mongoose.model('Task', new Schema({
name: String,
description:String
}))
// Create a task with random name
const task = new Task({
name: Math.random().toString(36).substring(7),
description: "THis is description of the task."
})
await task.save();
const tasks = await Task.find();
return tasks;
// Close the connection
//await mongoose.connection.close()
}
}
Has someone done this type of code?有人做过这种类型的代码吗? Please suggest me the edits in the above code.请建议我在上面的代码中进行编辑。
This could be due to a custom mongoDB configuration cause the default doesn't throw these kind of errors.这可能是由于自定义 mongoDB 配置导致默认不会引发此类错误。 You should double check your configuration file and check differences compared with the default.您应该仔细检查您的配置文件并检查与默认配置文件的差异。 I would suggest try passing your connect
method some options like: tlsInsecure: false
, secureProtocol: 'TLS_method'
or even tls: false
.我建议尝试传递您的connect
方法一些选项,例如: tlsInsecure: false
、 secureProtocol: 'TLS_method'
甚至tls: false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.