猫鼬5.0.16,无法读取未定义的属性“替换" [英] Mongoose 5.0.16, Getting Cannot read property 'replace' of undefined

查看:56
本文介绍了猫鼬5.0.16,无法读取未定义的属性“替换"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个新的Node,Express,Mongo应用程序,但是在设置MongoDB时突然出现了Mongoose错误.

I created a new Node, Express, Mongo app but suddenly I'm getting this error with Mongoose when setting up MongoDB.

Server Listening to Port: 9001 
C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:130
username = username.replace('=', '=3D').replace(',', '=2C');
                   ^

TypeError:无法读取未定义的属性替换"

TypeError: Cannot read property 'replace' of undefined

 at executeScram (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:130:24)
 at C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\auth\scram.js:299:7
 at _combinedTickCallback (internal/process/next_tick.js:73:7)
 at process._tickCallback (internal/process/next_tick.js:104:9) [nodemon] app crashed - waiting for file changes before starting...

我用于设置MongoDB的代码是:

my code for setting up MongoDB is:

const session    = require('express-session');
const mongoose   = require('mongoose');
const mongoStore = require('connect-mongo')(session);

// Local connection
let mongoConnectionLocal = { 
    'url': `mongodb://${process.env.MongoDBLocalUser}:${process.env.MongoDBLocalPassword}@127.0.0.1:27017/my-database`
};    

mongoose.connect(mongoConnectionLocal.url, {auth:{authdb:"admin"}}, err => { if(err) { console.log(err); }});

这太奇怪了,因为此代码可用于Mongoose 4.13.2及更低版本(我尝试卸载5.0.16版,然后安装4.13.2版,并且没有错误).在实现与Mongoose的MongoDB连接方面是否有任何变化?

This is so weird because this code work with Mongoose version 4.13.2 and lower (I tried uninstalling version 5.0.16 then installed the version 4.13.2 and it works without error). Has there any changes in implementing MongoDB connection with Mongoose?

更新:有人说使用默认值,使用默认值会产生此错误:

Update: Someone said use the default, Using the default creates this error:

 default: mongoose.connect(mongoConnectionLocal.url, err => { if(err) { console.log(err); }});

{ MongoError: Authentication failed.
at C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:598:61
at authenticateStragglers (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:516:16)
at Connection.messageHandler (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\pool.js:552:5)
at emitMessageHandler (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:309:10)
at Socket.<anonymous> (C:\Users\Sarwin\Desktop\Sarwin\Projects\SchoolProject\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:452:17)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Socket.Readable.push (_stream_readable.js:134:10)
at TCP.onread (net.js:551:20)
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
errmsg: 'Authentication failed.',
code: 18,
codeName: 'AuthenticationFailed' }
(node:20124) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): MongoError: Authentication failed.

推荐答案

已通过删除连接URI中的用户名和密码来解决此问题

This issue have been resolve by removing the username and password in the connection URI

与其以这种方式建立猫鼬连接,

instead of setting up mongoose connection this way,

const session    = require('express-session');
const mongoose   = require('mongoose');
const mongoStore = require('connect-mongo')(session);

// Local connection
let mongoConnectionLocal = { 
    'url':`mongodb://${process.env.MongoDBLocalUser}:${process.env.MongoDBLocalPassword}@127.0.0.1:27017/my-database`
};    

mongoose.connect(mongoConnectionLocal.url, {auth:{authdb:"admin"}}, err => { if(err) { console.log(err); }});

创建一个选项并在那里设置数据库用户名和密码,

Create an option and set the database username and password there,

var options = {
  auth: {authdb: 'admin'},
  user: process.env.MongoDBLocalUser,
  pass: process.env.MongoDBLocalPassword,
}

let mongoConnectionLocal = { 
    'url': `mongodb://127.0.0.1:27017/my-database`
}; 

mongoose.connect(mongoConnectionOnline.url, options, err => { if(err) { console.log(err); }}); 

它有效.

注意:有人在评论中说我使用了错误的凭据,但是我对旧版本和较新版本的猫鼬使用了相同的凭据,并且两者均有效.

其他人也遇到此问题.请参阅:

Others are also experiencing this issue. See: MongoDB Auth Fails to find username on Bitnami MEAN Stack Image

[参考] https://github.com/Automattic/mongoose/issues/4891

这篇关于猫鼬5.0.16,无法读取未定义的属性“替换"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆