简体   繁体   中英

mssql node, get request returns ReferenceError: request is not defined

working on rest api for my db got this problem

ReferenceError: request is not defined

right now I have a connection that works, but stuck with how to make rest requests with mssql-node. originally I used mySql db but now have to make it work with mssql. after I spent some time I have 2 error types - this one or connection doesn't work if I do connection in app.get

the way it's set up right now is

const express = require('express')
const sql = require('mssql');
const Connection = require('tedious').Connection;

var config = {
  userName: 'user' , 
  password: 'pass', 
  domain: "AD",
  server: serversIP,
  database: 'test',
  port: 2222,
  debug: true,
  driver: 'tedious',
  options: {
    database:"test",
    instanceName : "instance"
  } 
}
var connection = new Connection(config);
connection.on('connect', function(err) {
      if (err) {
        console.log(err);
      } else {
        console.log("server is connected to DB")
      }}
 );

app.get("/" , (req, res)=>{
    new sql.Request().query(`SELECT * FROM test `, (err, recordset) => {
      if (err) {
        console.log(err);
        return;
        }
      else {
        res.send(JSON.stringify(recordset));
        }
      });
    request.query();
    });

what can be the reason for this error?

UPDATE

after several small iterations of errors I got an error that connaction isn't specified

with new function like this

app.get("/getUsers", (req, res)=>{
  sql.connect(config, function (err) {
    var request = new sql.Request();
    if (err) {
        console.log(err);
        return;
    }
  new sql.query(`SELECT * FROM dbo.tblDatabaseUserIDs_lookup `, (err, recordset) => {
    console.log("first error msg");
    if (err) {
      console.log(err);
      return;
      }
    else {
      console.log("second error msg");
      res.send(JSON.stringify(recordset));
      }
    });
    console.log("3rd error msg");
  //req.query();
  });
}); 

my new error msg is

var bufferLength = 64 + domain.length * 2 + username.length * 2 + lm v2len + ntlmv2len + 8 + 8 + 8 + 4 + server_data.length + 4;

TypeError: Cannot read property 'length' of undefined

thought that it can be due to first connection function so i replaced

  sql.connect(config, function (err) {
    var request = new sql.Request();
    if (err) {
        console.log(err);
        return;
    }

with

...connection.callProcedure(function (err) {
    new sql.query(`SELECT * .... 

and got this

TypeError: request.validateParameters is not a function

but with

 connection.commitTransaction 

it gives me this one

(node:15956) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): RequestError: No connection is specified for that request.

the fun part that i tried to follow this solution Can't get Node mssql to work properly

This may help!! try to pass connection as an argument for Request(). var request = new sql.Request(connection)

a link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM