0
votes

    
    
    var _expressPackage = require("express");  
            var _bodyParserPackage = require("body-parser");  
            var _sqlPackage = require("mssql");  
        //Initilize app with express web framework  
            var app = _expressPackage();  
        //To parse result in json format  
            app.use(_bodyParserPackage.json());  
          
        ***//Here we will enable CORS, so that we can access api on cross domain.***  
            app.use(function (req, res, next) {  
            res.header("Access-Control-Allow-Origin", "*");  
            res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");  
            res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content- 
   Type, 
            Accept, Authorization");  
            next();  
             });  
          
        ***//Lets set up our local server now.***  
            var server = app.listen(process.env.PORT || 4000, function () {  
            var port = server.address().port;  
            console.log("App now running on port", port);  
           });  
          
        ***//Set up your sql connection string, i am using here my own, you have to replace it with your 
         own.***  
            var dbConfig = {  
            user: "sa1",  
            password: "12345",  
            server: "localhost",  
            database: "test123"  
              };  
          
        ***//Function to connect to database and execute query***  
                  var QueryToExecuteInDatabase = function (response, strQuery) {  
            ***//close sql connection before creating an connection otherwise you will get an error if 
             connection already exists.***  
                   _sqlPackage.close();  
            //Now connect your sql connection  
                   _sqlPackage.connect(dbConfig, function (error) {  
                    if (error) {  
                       console.log("Error while connecting to database :- " + error);  
                       response.send(error);  
                    }  
                    else {  
                    ***//let's create a request for sql object***  
                        var request = new _sqlPackage.Request();  
                    //Query to run in our database  
                        request.query(strQuery, function (error, responseResult) {  
                            if (error) {  
                               console.log("Error while connecting to database:- " + error);  
                               response.send(error);  
                            }  
                            else {  
                                response.send(responseResult);  
                            }  
                        });  
                    }  
                });             
             }  
          
        ***//GET API***  
            app.get("/StudentList", function(_req ,_res){  
            var Sqlquery = "select * from student1";  ***//tbl_studentdetails***
            QueryToExecuteInDatabase(_res, Sqlquery);  
            });
    
        ***//call a stored procedure***
            var request = new _sqlPackage.Request();  
           ***//calling a stored procedure***  
                        request.input('Username', _sqlPackage.VarChar(50), 'admin');  
                        request.input('Password', _sqlPackage.VarChar(50), 'admin@123');  
                        request.execute('sp_CheckLogin', function (err, recordsets, returnValue) {  
                            response.send(recordsets);  
                        });
    
    

>  (D:\performalytic\9999.practice\angularpra\NodeApiWithSql\node_modules\mssql\lib\tedious\request.js:701:23)
    at processImmediate (internal/timers.js:463:21)
- end snippet -->

1

1 Answers

0
votes

This question could use a bit more clarity... but with the limited information provided it appears the issue you are running into here has to do with lexical scope.

Lexical scope most simply has to do with what variables the current execution context has access to. Inside of a function, you can either access variables declared within the function... or in the surrounding code. The last line of your code snipped shows a top level variable request and a method on that object called execute.

The callback you are passing the execute method has three variables (function parameters) you're naming err, recordsets, and returnValue. Yet inside that function body you're attempting to access a variable named response. If we look in the surrounding code... there is no response variable declared. (The only variable named response I see is within the QueryToExecuteInDatabase, and therefore only accessible within that function body.

Where are you getting this templated code from?