tediousjs / tedious

Node TDS module for connecting to SQL Server databases.
http://tediousjs.github.io/tedious/
MIT License
1.57k stars 436 forks source link

URL Parsing error #1640

Closed freercurse closed 3 months ago

freercurse commented 3 months ago

Software versions

Additional Libraries Used and Versions N/A

Table schema

router.get('/types', (req,res) => {
  const table = '[My].[Table].[Name]'
  const sql = `select * from ${table}`

  const request = new Request(sql,
  (err, rowCount, rows) => {
    if(err) {
      res.status(500).send("Internal Server Error: " + err)
    } else {
      res.json(rows)
    }

    req.connection.close()
  })

  req.connection.execSql(request)
});

Connection configuration // Paste your connection config here.

const config = {
    "server": "SERVER_NAME",
    "authentication": {
      "type": "default",
      "options": {
        "userName": process.env.DB_UN,
        "password": process.env.DB_PW
      }
    },
    "options": {
      "port": 1433,
      "database": "DB_NAME",
      "trustServerCertificate": true
    }
}

Problem description regardless of the way I write out the Table name, brackets, no brackets, plain string, adding database name, not adding db name. i have confirmed that the query and URL works perfectly through the VS CODE SQL Server extension.

Expected behavior the URL is parsed correctly, it finds the tables and correctly executes the query

Actual behavior

TypeError: Failed to parse URL from select * from [My].[Table].[Name]
    at new Request (node:internal/deps/undici/undici:4855:19)

Error message/stack trace ^^

Any other details that can be helpful N/A

freercurse commented 3 months ago

after some more work, I found the issue. when using a middleware JS file to pass the connection object to each route, the connection.on'connect' needs to call the request.exec() to ensure that correct the correct state is achieved

MichaelSun90 commented 3 months ago

Glad to hear that you got the issue resolved. I will close this one for now. If any further help needed , do not hesitate to post a new one.