Error connecting to Azure: Illegal character in password with mongoose 5.0.1 but works in 4.13.9

后端 未结 4 942
鱼传尺愫
鱼传尺愫 2021-02-14 07:11

I have a node.js application that is deployed to azure using CosmosDB and the MongoDB API. My application uses mongoose which works seamlessly in 4.13.9.

My application

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 07:34

    To connect to local cosmos db emulator use the following connection method (for mongoose > 5.0.0):

      mongoose.connect(
    
      `mongodb://localhost:10255/?ssl=true`,
      {
        auth: {
          user: "localhost",
          password: "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==",
          dbName: "admin"
        }
      }
    );
    

    Or you may also do the following:

    const encodedPassword = encodeURIComponent("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==");
    mongoose.connect(`mongodb://localhost:${encodedPassword}@localhost:10255/admin?ssl=true`);
    

    Connection string has following format:

    mongodb://username:password@host:port/[database]?ssl=true

    and there seems to be some issue with default password character escaping. Thus we encoded it separately.

提交回复
热议问题