Get the App
SLTechnology News&Howtos  ›  Database  › 

About the trap of mongoose certification [url parameters are not recognized]

Shulou Source: shulou.com Published: 2022-06-01 05:01:43 09月17日 Update

Http://cnodejs.org/topic/57baa91ddcaeb5d932db223b

# connection code var conn = mongoose.connect ('mongodb://username:password@127.0.0.1:27017/test?authMechanism=MONGODB-CR')

# error > MongoError: server 127.0.0.1 timed out 27017

# try to connect with the command line > mongo-u username-p password 127.0.0.1 MongoDB shell version: 2.4.9 connecting to: 127.0.0.1:27017/test Error while trying to show server startup warnings: not authorized on admin to execute command {getLog: "startupWarnings"}

The connection is successful and there is no problem with performing read and write operations. Could it be a problem with mongoose? # then test it with the mongoDB driver.

Var MongoClient = require ('mongodb'). MongoClient; var url =' mongodb://username:password@127.0.0.1:27017/test?authMechanism=MONGODB-CR'; MongoClient.connect (url, function (err, db) {console.log (err? Err: 'mongoDB connection succeeded!');})

> node mongoDB.js mongoDB connection succeeded!

Since mongoose is based on mongoDB drivers, this is unlikely to happen. Could it be that mongoose does not recognize the url parameter. Next, remove the string MongoError: server 127.0.0.1 timed out 27017 timed out from'? authMechanism=MONGODB-CR'. Sure enough, mongoose could not recognize this parameter, and then looked through the document [mongoose document] http://mongoosejs.com/docs/api.html Ctrl+F paste authMechanism and couldn't find it. Click connection.js directly to find this line of auth-options for authentication (see http://mongodb.github.com/node-mongodb-native/api-generated/db.html#authenticate) clicks in to find what we want.

Modify the code to configure options as Object!

Const options = {user: "username", pass: "password", auth: {authMechanism: 'MONGODB-CR'}} var conn = mongoose.connect (' mongodb://@127.0.0.1:27017/test',options)

Rerun node app.js [2016-08-22 15 console 21 app listen to 39.709] [INFO] console-app listen to 3000, NODE_ENV: development perfect! Well, it's healthier to step on, especially for us newcomers.

Tags: Success next parameters code documentation problem driver health one line small command character string situation novice way try test run configure Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Information Shulou Tech Info Redmi Huawei