简体   繁体   English

在 Vercel 和 Netlify 上部署 next.js 项目时如何解决 Runtime.UnhandledPromiseRejection 错误?

[英]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 uri parameter 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 uri parameter 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 uri parameter to must be a string, got \"undefined\". Make sure the first parameter to or mongoose.createConnection() is a string.","trace":["Runtime.UnhandledPromiseRejection: MongooseError: The uri parameter 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.

相关问题 使用 replaceAll 将 Next.js 应用程序部署到 Vercel 时出错 function - Error when deploying Next.js app to vercel with replaceAll function 将 next.js 站点部署到 Netlify 时,如何解决“找不到模块:无法解析 [目录] 中的 [css 文件]”? - How do I resolve the "Module not found: Can't resolve [css file] in [directory]" when deploying next.js site to Netlify? 在 Netlify 上部署 next.js 时无法解决此错误。 导入的图像和标签引发错误 - Can't solve this error in next.js Deploying on Netlify. Imported Image and tag throwing the error 使用 getStaticProps() 将 Next.js 部署到 Vercel 的速率限制问题 - Rate limit issue deploying Next.js to Vercel using getStaticProps() 将 next.js 应用程序部署到 vercel 时如何避免/控制时区偏移? - How to avoid/control a timezone offset when deploying next.js app to vercel? Next.js 项目的 Vercel 部署因意外令牌“导出”而失败 - Vercel Deployment of Next.js project is failing with Unexpected token 'export' 预呈现页面“/”时发生错误。 部署错误 Vercel | Next.js - Error occurred prerendering page "/". Deployment Error Vercel | Next.js 在 Next.js 中获取非 API 数据的最佳实践是什么(通过 Vercel 部署) - What is the best practice to fetch non-api data in Next.js(deploying through Vercel) Vercel 上的 Next.js 部署失败 - Failing Next.js deployment on Vercel Vercel next.js 环境变量不起作用 - Vercel next.js environment variable not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM