简体   繁体   English

尝试连接到MongoDB Atlas时如何解决密码错误?

[英]How to fix password error when trying to connect to MongoDB Atlas?

I keep getting an error when trying to connect to mongdb atlas from my Node application 尝试从Node应用程序连接到mongdb atlas时,我一直收到错误mongdb atlas

Here is my code 这是我的代码

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');

const url = `mongodb+srv://username:passwordwithspeciacharacters$$!!22@cluster0-xxxxx.mongodb.net/test?retryWrites=true&w=majority`;

module.exports = {
  signup: (name, email, password) => {
    MongoClient.connect(url, (err, db) => {
      if (err) {
        console.log(err);
      } else {
        db.collection('user').insertOne({
            name: name,
            email: email,
            password: password
          },
          function(err, result) {
            assert.equal(err, null);
            console.log('Saved the user sign up details.');
          }
        );
      }
    });
  }
};

Here is the error I am getting 这是我得到的错误

(node:9626) DeprecationWarning: current URL string parser is 
deprecated, and will be removed in a future version. To use the new 
parser, pass option { useNewUrlParser: true } to MongoClient.connect.
Error: Password contains an illegal unescaped character

How should I sanitize may password I am passing in? 我应该如何清除可能传入的密码?

const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const crypto = require('crypto');

const url = `mongodb+srv://username:passwordwithspeciacharacters$$!!22@cluster0-xxxxx.mongodb.net/test?retryWrites=true&w=majority`;

module.exports = {
    signup: (name, email, password) => {
        MongoClient.connect(url ,{useNewUrlParser:true}, (err, db) => {
            if (err) {
                console.log(err);
            } else {
                db.collection('user').insertOne({
                    name: name,
                    email: email,
                    password: crypto
                        .createHmac('sha256', 'secret')
                        .update(password)
                        .digest('hex')
                },
                    function (err, result) {
                        assert.equal(err, null);
                        console.log('Saved the user sign up details.');
                    }
                );
            }
        });
    }
};

// Same way you can match password while login //与登录时匹配密码的方式相同

MongoDB passwords are URL-encoded, so you should use encodeURIComponent . MongoDB密码是URL编码的,因此您应该使用encodeURIComponent Add the useNewUrlParser options object to fix the warning as well. 添加useNewUrlParser选项对象也可以修复警告。

MongoClient.connect(encodeURIComponent(url), { useNewURLParser: true }, (err, db) => {...});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确连接到 mongodb.atlas? Mongo网络错误 - how to properly connect to mongodb.atlas? MongoNetworkError mongoDB 错误:无法连接到 MongoDB Atlas 集群中的任何服务器 - mongoDB error : Could not connect to any servers in your MongoDB Atlas Cluster 尝试运行 AudioContext.createMediaElementSource() 时如何解决“Audion 无法识别调用‘connect’的对象”错误? - How to fix "Audion could not identify the object calling 'connect'" error when trying to run AudioContext.createMediaElementSource()? 如何从Firebase云功能连接到MongoDb Atlas - How to connect to MongoDb Atlas from firebase cloud functions 尝试与angularJS $ http连接时出现mongodb服务器错误 - mongodb server error when trying to connect with angularJS $http Heroku 无法连接到我的 mongodb 地图集 - Heroku couldn't connect to my mongodb atlas 无法在 MERN 项目中将 mongodb 地图集与 nodejs 连接 - unable to connect mongodb atlas with nodejs in a MERN project 为什么我无法连接到 mongoDB 图集? - Why can't I connect to mongoDB atlas? Sails.js 连接云 mongoDb 图集 - Sails.js connect to Cloud mongoDb Atlas 出现错误:querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net。 当我离线时尝试连接到 localhost - Getting Error: querySrv ECONNREFUSED _mongodb._tcp.auctiondbcluster.s6rzg.mongodb.net. When trying to connect to localhost when I am offline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM