最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Next.js API routes with SQL Server database work locally but fail after deploying to Vercel - Stack Overflow

programmeradmin0浏览0评论

I am building a nextjs self project with API routes defined in it and using the mssql npm package to write queries in the API route to fetch and modify data from the database. My project works perfect locally but having issue after deploying it on vercel the UI renders but none of the API's work in order to fetch information from the SQL Server database.

This is my database connection function which ensures I am connected with my database through connection string:

import sql from "mssql";
import connectionString from "./database";

let connectionPool = null;

const dbConnect = async () => {
  if (!connectionPool) {
    try {
      connectionPool = await sql.connect(connectionString);
      console.log("Connected successfully to the database");
    } catch (error) {
      console.error("Error while connecting to the database:", error.message);
      throw error;
    }
  }
  return connectionPool;
};

export default dbConnect;

and this is my connection string and I have added all the env variables in Vercel env while deploying as key value pairs

const connectionString = {
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  server: process.env.SERVER_NAME,
  database: process.env.DB_NAME,
  pool: {
    max: 10,
    min: 0,
    idleTimeoutMillis: 30000,
  },
  options: {
    encrypt: false,
    trustServerCertificate: true
  },
};

export default connectionString;

when I checked my vercel logs after registering a user through ui i got this

Error while connecting to the database: getaddrinfo EBUSY ayaanshaikh"

Error while registering user: Error [ConnectionError]: getaddrinfo EBUSY ayaanshaikh
    at <unknown> (/var/task/todolist/.next/server/chunks/344.js:76:290113)
    at H.t (/var/task/todolist/.next/server/chunks/344.js:76:78788)
    at H.emit (/var/task/todolist/.next/server/chunks/344.js:76:78979)
    at <unknown> (/var/task/todolist/.next/server/chunks/344.js:76:79583) {
  code: 'EINSTLOOKUP',
  originalError: Error: getaddrinfo EBUSY ayaanshaikh
      at <unknown> (/var/task/todolist/.next/server/chunks/344.js:76:79598) {
    code: 'EINSTLOOKUP',
    [cause]: [Error: getaddrinfo EBUSY ayaanshaikh] {
      errno: -16,
      code: 'EBUSY',
      syscall: 'getaddrinfo',
      hostname: 'ayaanshaikh'
    }
  }
}

I have tried asking chatgpt but no help was provided please guide me through this.

发布评论

评论列表(0)

  1. 暂无评论