TypeError:“ config.server”属性是必需的,并且在NodeJS中必须为字符串类型

时间:2020-05-20 16:37:07

标签: javascript node.js sql-server backend

我正在尝试使用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:(

0 个答案:

没有答案