tediousjs / node-mssql

Microsoft SQL Server client for Node.js
https://tediousjs.github.io/node-mssql
MIT License
2.23k stars 466 forks source link

The "config.server" property is required and must be of type string. #1617

Closed smaranneducations closed 6 months ago

smaranneducations commented 6 months ago

I have three layers
DBconfiguration object, function which uses the configuration, and API that uses the function.

when i pass the values to db configuration as a variable , dowes not workd with api, error The "config.server" property is required and must be of type string. works with function call when i pass the values to variable hardcoded workd with api works with function call

I should have got my response in both the cases, wether i use variable or hard code for the configuration, as my consle.log for the variable prints the same string

Configuration:

import dotenv from 'dotenv';
let result = dotenv.config();

const user = process.env.DB_USER;
console.log('result:', user);

const mssqlconfig = {
    user: "admin",
    password: "Welcome123!",
    server: "localhost",
    database: "ITSMDashboard",
    options: {
        trustedConnection: false, // Change to true if using Windows authentication
        trustServerCertificate: true
    },
    pool: {
        max: 10,
        min: 0,
        idleTimeoutMillis: 30000
    }
};

export default mssqlconfig;

// API endpoint to fetch data from a given table
ScenarioRouter.get('/api/fetchdata/:tableName', async (req, res) => {
  try {
    const tableName = req.params.tableName;
    const data = await fetchData(tableName);
    console.log('data:', data);
    res.json(data);
  } catch (error) {
    res.status(500).send(error.message); 
  }
});
async function getColumnNames(tableName) {
    try {
        await sql.connect(mssqlconfig);
        const query = `SELECT COLUMN_NAME, DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${tableName}'`;
        const result = await sql.query(query);
        if (result.recordset.length === 0) {
            throw new Error(`Table '${tableName}' does not exist.`);
        }
        const columns = result.recordset.map(row => ({
            name: row.COLUMN_NAME,
            dataType: row.DATA_TYPE
        }));
        console.log('Columns:', columns);
        return columns;

    } catch (err) {
        throw err;
    } finally {
        sql.close();
    }
}
// API endpoint to fetch data from a given table
ScenarioRouter.get('/api/fetchdata/:tableName', async (req, res) => {
  try {
    const tableName = req.params.tableName;
    const data = await fetchData(tableName);
    console.log('data:', data);
    res.json(data);
  } catch (error) {
    res.status(500).send(error.message); 
  }
});

Software versions

dhensby commented 6 months ago

I'm not sure this is something I can help with as it sounds like an issue with your application. You've said when you hard code the value it works, so it's not a problem with this library.

Good luck