Invalid scheme, expected connection string to start with “mongodb:” or “mongodb+srv://”

huangapple go评论79阅读模式
英文:

Invalid scheme, expected connection string to start with "mongodb:" or "mongodb+srv://

问题

部署我的API,然后尝试向数据库发送GET请求会导致Runtime.UnhandledPromiseRejection错误,错误消息为Invalid scheme, expected connection string to start with "mongodb:" or "mongodb+srv://

我的db.js文件如下:

import mongoose from "mongoose";
mongoose.Promise = global.Promise;
mongoose.set("strictQuery", false);

let connection_uri = process.env.MONGODB_URI;
let cachedMongoConn = null;

export const connectToDatabase = () => {
  return new Promise((resolve, reject) => {
    mongoose.connection
      .on("error", (error) => {
        console.log("Error: connection to DB failed");
        reject(error);
      })
      .on("close", () => {
        console.log("Error: Connection to DB lost");
        process.exit(1);
      })
      .once("open", () => {
        const infos = mongoose.connections;
        infos.map((info) =>
          console.log(`Connected to ${info.host}:${info.port}/${info.name}`)
        );
        resolve(cachedMongoConn);
      });
    if (!cachedMongoConn) {
      cachedMongoConn = mongoose.connect(connection_uri, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        connectTimeoutMS: 60000,
        bufferCommands: false,
      });
    } else {
      console.log("MongoDB: using cached database instance");
      resolve(cachedMongoConn);
    }
  });
};

.env文件内容如下:

MONGODB_URI=mongodb+srv://Username:Password:...

看起来一切“看起来”都是正确的,.env文件中的连接字符串格式也正确。因此,我不确定问题出在哪里。

如果您需要进一步的帮助,请告诉我。

英文:

Deploying my API then attempting to send a GET request to the DB results in Runtime.UnhandledPromiseRejection Invalid scheme, expected connection string to start with "mongodb:" or "mongodb+srv://

My db.js

import mongoose from "mongoose";
mongoose.Promise = global.Promise;
mongoose.set("strictQuery", false);

let connection_uri = process.env.MONGODB_URI;
let cachedMongoConn = null;

export const connectToDatabase = () => {
  return new Promise((resolve, reject) => {
    // mongoose.Promise = global.Promise;
    mongoose.connection
      .on("error", (error) => {
        console.log("Error: connection to DB failed");
        reject(error);
      })
      .on("close", () => {
        console.log("Error: Connection to DB lost");
        process.exit(1);
      })
      // Connected to DB
      .once("open", () => {
        // Display connection information
        const infos = mongoose.connections;

        infos.map((info) =>
          console.log(`Connected to ${info.host}:${info.port}/${info.name}`)
        );
        // Return successful promise
        resolve(cachedMongoConn);
      });
    if (!cachedMongoConn) {
      cachedMongoConn = mongoose.connect(connection_uri, {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        connectTimeoutMS: 60000,
        bufferCommands: false, 
      });
    } else {
      console.log("MongoDB: using cached database instance");
      resolve(cachedMongoConn);
    }
  });
};

.env

MONGODB_URI=mongodb+srv://Username:Password:...

Everything "looks" correct and the connection string in the .env file is in the right format. So I'm not sure what the issue is.

Any help you can provide would be greatly appreciated.

答案1

得分: 1

I just had this same issue, removing the quotes before the URI link, solved it.

The problem is that the variable is most likely not formatted correctly.

Remove the spaces, the quotes, and the semi-colon at the end.

This


CONNECTION_URL =
"mongodb+srv://<UserName>:<Password>@cluster0.v5qzigz.mongodb.net/?retryWrites=true&w=majority";

Becomes this


CONNECTION_URL=mongodb+srv://<UserName>:<Password>@cluster0.v5qzigz.mongodb.net/?retryWrites=true&w=majority

credits to dev mikeym

Then the connection should work

英文:

I just had this same issue, removing the quotes before the URI link, solved it.

The problem is that the variable is most likely not formatted correctly.

Remove the spaces, the quotes, and the semi-colon at the end.

This


CONNECTION_URL =
"mongodb+srv://<UserName>:<Password>@cluster0.v5qzigz.mongodb.net/?retryWrites=true&w=majority";

Becomes this


CONNECTION_URL=mongodb+srv://<UserName>:<Password>@cluster0.v5qzigz.mongodb.net/?retryWrites=true&w=majority

credits to dev mikeym

Then the connection should work

huangapple
  • 本文由 发表于 2023年1月5日 10:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75013217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定