英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论