簡體   English   中英

在 Vercel 和 Netlify 上部署 next.js 項目時如何解決 Runtime.UnhandledPromiseRejection 錯誤?

[英]How to resolve Runtime.UnhandledPromiseRejection error while deploying the next.js project on Vercel and Netlify?

我在 Vercel / Netlify 上部署它時收到此錯誤。 部署時 Vercel 和 Netlify 收到相同的錯誤。 解決此問題的任何步驟都會有所幫助。

數據庫代碼:

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;

錯誤截圖

在此處輸入圖像描述

錯誤:

( {"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)從 mongoDB 檢查你的連接字符串。在你的.env文件中並將它與來自圖集的數據進行比較

你的用戶是正確的? 你的密碼正確嗎? 你的集群名稱是一樣的? 你的數據庫名稱是一樣的?

1a) 確保您的.env在根文件夾中!

2)在mongoDB Atlas中查看網絡訪問IP。 活躍嗎? 如果沒有,建立新的。

3) 如果您使用的是VPN ,請將其關閉。

4) 最后 =>https://vercel.com/docs/concepts/projects/environment-variables

我希望它能幫助你; 祝你好運 ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM