gpt4 book ai didi

node.js - Mongoose Auth Error : command find requires authentication. 但我做到了

转载 作者:行者123 更新时间:2023-12-04 17:38:19 26 4
gpt4 key购买 nike

我试图找到一个我知道存在的文档,因为我可以在管理控制台中看到它,这要归功于 mongo-express...但是当我使用 Model.findOne( 时,它会吐出下面的内容错误消息。我做错了什么?(主机名“mongo”是我的 docker 容器,它连接得很好,我可以在日志中看到它)

这是我的代码:

// This all works because the following events show success, and no error.
// So I know it's authing correctly on initial connection
-dbConnect.js-
"user strict"

import mongoose from "mongoose"

const connection = mongoose.createConnection(
"mongodb://mongo:27017/dbName?authSource=admin", {
useNewUrlParser: true,
user: process.env.user,
pass: process.env.pass,
keepAlive: true,
})
connection.on( "connected", () => {
console.log( "MONGOOSE: connected" )
})

connection.on( "close", () => {
console.log( "MONGOOSE: connection close" )
})

connection.on( "error", error => {
console.log( "MONGOOSE: connection error", error )
})

export default connection

-

-models/auth.js-
"use strict"

import mongoose from "mongoose"
import connection from "../dbConnect"

const xSchema = new mongoose.Schema(
{
id : String,
user : String,
pass : String
},
{ collection: "x" }
)
export const X = connection.model( "X", xSchema )

-

-controllers/auth.js-
import { X } from "../models/auth"
//Promise wrapper
X.findOne( { id: incommingId }, ( error, x ) => {
//handler
})

最后我遇到了这个错误

MongoError: command find requires authentication
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:443:61)
at Connection.emit (events.js:189:13)
at Connection.EventEmitter.emit (domain.js:441:20)
at processMessage (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:364:10)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:533:15)
at Socket.emit (events.js:189:13)
at Socket.EventEmitter.emit (domain.js:441:20)
at addChunk (_stream_readable.js:284:12)
at readableAddChunk (_stream_readable.js:265:11)
at Socket.Readable.push (_stream_readable.js:220:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

最佳答案

答案简单而愚蠢。上面的一切都是正确的...留下未定义的环境变量。

显然,mongo 允许您在未经授权的情况下进行连接,您什么也做不了。正确填充这些环境变量,一切都进展顺利。

关于node.js - Mongoose Auth Error : command find requires authentication. 但我做到了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55642940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com