首页 > 解决方案 > TypeError:“config.server”属性是必需的,并且必须是 NodeJS 中的字符串类型

问题描述

我正在尝试使用 NodeJS 中的 mssql 包从 MSSQL 数据库中获取查询。当我运行我的代码时,会出现错误。另外问题是该错误仅出现在生产中,在开发中它可以正常工作!:(

这是我的代码

const { Router } = require('express');
const { config } = require("../database/config");
const sql = require("mssql");

router.post("/", (req, res) => {
 const {data1, data2} = req.body
 const query = `The Query is too long but it works!`

 switch (data1) {
 case 'subdata1':

// Making the connection to MMSQL
  const pool1 = new sql.ConnectionPool(Config);
  const poolConnect = await pool1.connect() // The trace error indicates that the problem is in this line
                             //  ^^^ exactly here
       async function data1Path() {
        await poolConnect;  
         try {
           const request = await pool1.request();
           const result = await request.query(query);
           // Handling the query result
           return result.recordset[0].Direccion.replace(/\\/g, "/");
         } catch (error) {
           console.log("SQL Error: ", error);
         };
       };
       const getData1Query = await data1Path(); // Getting the path to find the file
 }
 case 'subdata2':
  // doing another things...

})

下一个代码是我的数据库配置文件,我用 module.exports 导出它:

const config = {
  server: DB_HOST,
  port: 1433,
  user: DB_USER,
  password: DB_PASSWORD,
  database: "my_db_name",
  connectionTimeout: 180000,
  driver: "tedious",
  stream: "true",
  options: {
    appName: APP_NAME,
    encrypt: false,
    enableArithAbort: true,
  },
  pool: {
    max: 70,
    min: 0,
    idTimeoutMillis: 30000,
    log: true,
  },
};

我一直在测试和测试,我找不到什么问题。我希望你能帮助我,我会非常感激<3 :(

标签: javascriptnode.jssql-serverbackend

解决方案


推荐阅读