[英]How to resolve Runtime.UnhandledPromiseRejection error while deploying the next.js project on Vercel and Netlify?
I'm receiving this error while deploying it on Vercel / Netlify.我在 Vercel / Netlify 上部署它时收到此错误。 Receiving the same error for both Vercel and Netlify while deployment.
部署时 Vercel 和 Netlify 收到相同的错误。 Any steps to resolve this issue would be helpful.
解决此问题的任何步骤都会有所帮助。
Database code:数据库代码:
import mongoose from 'mongoose';
const connection = {};
async function connect() {
if (connection.isConnected) {
console.log('already connected');
return;
}
if (mongoose.connections.length > 0) {
connection.isConnected = mongoose.connections[0].readyState;
if (connection.isConnected === 1) {
console.log('use previous connection');
return;
}
await mongoose.disconnect();
}
const db = await mongoose.connect(process.env.MONGODB_URI, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
});
console.log('new connection');
connection.isConnected = db.connections[0].readyState;
}
async function disconnect() {
if (connection.isConnected) {
if (process.env.NODE_ENV === 'production') {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log('not disconnected');
}
}
}
function convertDocToObj(doc) {
doc._id = doc._id.toString();
doc.createdAt = doc.createdAt.toString();
doc.updatedAt = doc.updatedAt.toString();
return doc;
}
const db = { connect, disconnect, convertDocToObj };
export default db;
Error Screenshot错误截图
Error:错误:
(
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongooseError: The
uriparameter to
openUri()must be a string, got \"undefined\". Make sure the first parameter to
mongoose.connect()or
mongoose.createConnection()is a string.","trace":["Runtime.UnhandledPromiseRejection: MongooseError: The
uriparameter to
openUri()must be a string, got \"undefined\". Make sure the first parameter to
mongoose.connect()or
mongoose.createConnection()is a string."," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
)(
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongooseError: The
uriparameter to
must be a string, got \"undefined\". Make sure the first parameter to
or
mongoose.createConnection()is a string.","trace":["Runtime.UnhandledPromiseRejection: MongooseError: The
uriparameter to
must be a string, got \"undefined\". Make sure the first parameter to
mongoose。 connect()or
mongoose.createConnection()is a string."," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:314:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
)
1)Checkout your connection string from mongoDB.In your .env
file and compare it with the data from the atlas 1)从 mongoDB 检查你的连接字符串。在你的
.env
文件中并将它与来自图集的数据进行比较
your user is correct?你的用户是正确的? your password is correct?
你的密码正确吗? your cluster name is the same?
你的集群名称是一样的? your database name is the same?
你的数据库名称是一样的?
1a)Make sure that your .env
is in the root folder! 1a) 确保您的
.env
在根文件夹中!
2)Checkout Network Access IP in mongoDB Atlas. 2)在mongoDB Atlas中查看网络访问IP。 Is it active?
活跃吗? If not, establish the new one.
如果没有,建立新的。
3)If you are using VPN turn it off. 3) 如果您使用的是VPN ,请将其关闭。
4)And lastly =>https://vercel.com/docs/concepts/projects/environment-variables 4) 最后 =>https://vercel.com/docs/concepts/projects/environment-variables
I hope it helps you;我希望它能帮助你; Good luck ;-)
祝你好运 ;-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.