0

I can't seem to make multiple requests from one connection to the database. It always tells me that requests can only be made from logged in state.

For example as seen in the code below: the getCarIdandOwner part of the function will fire fine. However the getChargeRate will not.

I tried combining them like so:

connection.execSqlBatch(getcarIdandOwner, getChargeRate);

However that did not work either as it told me that getChargeRate was not defined.

Using Visual Studio Community, have NPM: Underscore and Tedious (for sql) installed. Just running it as a console app for now to test.

var Connection = require('tedious').Connection;
var config = {
    userName: 'user',
    password: 'passs',
    server: 'somewhere.database.windows.net',
    options: {
        encrypt: true,
        database: 'db-Test',
        useColumnNames: true
var connection = new Connection(config);

connection.on('connect', function (err) {
    // If no error, then good to proceed.  
    console.log("Connected".green);
    toll("******-b77c-40e0-8f26-d44e98bc7264", "be45c903-****-****-b6ba-4b2fefa3d6b0");
}); 

function toll(chipId, locId) {
    var carId = '';
    var userOwner = '';
    var charge = '';
    var userBalance = '';

    getcarIdandOwner = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function (err) {
        if (err) {
            console.log(err);
        }
    });

    getcarIdandOwner.on('row', function (columns) {
        carId = columns.car_id.value;
        userOwner = columns.userOwner.value;
        console.log('carId: ', carId, ' userOwner: ', userOwner);
    });
    getcarIdandOwner.on('done', function (rowCount, more) {
        console.log(rowCount + ' rows returned');
        if (rowCount = 1) {
            console.log('Car Rows Returned Ok'.green);
        } else {
            console.log('Fatal Error: More than 1 Car Row Returned'.red);
        };
    });
    connection.execSqlBatch(getcarIdandOwner);

    getChargeRate = new Request(`SELECT Charge FROM locations WHERE location_id = '${locId}'`, function (err) {
        if (err) {
            console.log(err);
        }
    });
    getChargeRate.on('row', function (columns) {
        charge = columns.charge.value;
        console.log('Charging account: ', userOwner, '$', charge);
    });

    connection.execSqlBatch(getChargeRate);
}

1 Answer 1

1

There is some documentation at http://tediousjs.github.io/tedious/api-connection.html which states:

Only one request at a time may be executed on a connection. Once a Request has been initiated (with callProcedure, execSql, or execSqlBatch), another should not be initiated until the Request's completion callback is called.

So your code should be someting like this:

function toll(chipId, locId) {
    var carId = '';
    var userOwner = '';
    var charge = '';
    var userBalance = '';

    getcarIdandOwner = new Request(`SELECT car_id, userOwner FROM Cars WHERE carChipId = '${chipId}'`, function (err) {
        if (err) {
            console.log(err);
        } else {
            getChargeRate = new Request(`SELECT Charge FROM locations WHERE location_id = '${locId}'`, function (err) {
                if (err) {
                    console.log(err);
                }
            });
            getChargeRate.on('row', function (columns) {
                charge = columns.charge.value;
                console.log('Charging account: ', userOwner, '$', charge);
            });
            connection.execSql(getChargeRate);
        }
    });

    getcarIdandOwner.on('row', function (columns) {
        carId = columns.car_id.value;
        userOwner = columns.userOwner.value;
        console.log('carId: ', carId, ' userOwner: ', userOwner);
    });
    getcarIdandOwner.on('done', function (rowCount, more) {
        console.log(rowCount + ' rows returned');
        if (rowCount = 1) {
            console.log('Car Rows Returned Ok'.green);
        } else {
            console.log('Fatal Error: More than 1 Car Row Returned'.red);
        };
    });
    connection.execSqlBatch(getcarIdandOwner);

}
Sign up to request clarification or add additional context in comments.

4 Comments

Would the .on('done' be the completion Callback? Or is it just when the request returns (ideally) without an error in the if else
It should be in the Request's completion callback: new Request('sql', function (err, rowCount) {//here...}. BTW, you can also move the code in the Event: done section to this callback function.
Hey I have a few questions about some code, is there anyway I could contact you directly since you seem knowledgeable
Hi, you'd better post new questions on Stackoverflow where has a massive community of developers, who may be able to answer your question or give suggestions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.