1

I'm having a problem with my express and sql query code. I can't get the value of results of the sql query below but this works when logging the results. I've already return the results here.

Here's my sql.js code

var dataContext = require('node2sql')({
    username: 'user',
    password: 'password',
    server: '1xx.xx2.xx.xx9',
    options: {
        database: 'grades'
    }
});

module.exports = {
    DataQuery: function(query) {
        query = query.replace(/\s+/g, '').split("/");
        year = query[2].substr(2, 4);
        id = query[0];
        period = query[1].toUpperCase();

        if (period == 'SEM1') {
            period = "1st Semester";
        } else if (period == "SEM2") {
            period = "2nd Semester";
        }else if (period == 'SUM') {
            period = "Summer";
        }

        var queryStr = "SELECT SubjectCode,Grade FROM dbo.grade_list_" + year + " WHERE StudentNo=\'" + id + "\' and Semester=\'" + period + "\'";

        console.log(queryStr);

        dataContext.query(queryStr, function(err, results) {
            console.log(results)
            return results;
        });

    }
}

Here's my index page, as you can see I wanted to get the return value of data and show it to my index page (just a test only).

var express = require('express');
var sql = require('../libs/sql');

var router = express.Router();

router.get('/', function(req, res, next) {

    var data = sql.DataQuery("01-666-123/sem2/2009")
    res.end(data);

});

module.exports = router;

I accept any of your suggestions guys. I've already read this question which was asked before but still I can't figure out how to make this work. Thanks

3
  • I can't help but notice that your DataQuery function takes a fn argument, but doesn't do anything with it. It seems like that might be part of the solution. Commented Feb 8, 2017 at 4:38
  • I didn't noticed it. Still doesn't work. I'll edit the code above. Thanks Commented Feb 8, 2017 at 5:14
  • You need to pass that callback function... Check answer below... Commented Feb 8, 2017 at 5:28

2 Answers 2

2

You cannot return from a callback. (Callbacks are asynchronous and can complete at any time.) Try this:

sql.js

var dataContext = require('node2sql')({
    username: 'user',
    password: 'password',
    server: '1xx.xx2.xx.xx9',
    options: {
        database: 'grades'
    }
});

module.exports = {
    DataQuery: function(query, callback) {
        query = query.replace(/\s+/g, '').split("/");
        year = query[2].substr(2, 4);
        id = query[0];
        period = query[1].toUpperCase();

        if (period == 'SEM1') {
            period = "1st Semester";
        } else if (period == "SEM2") {
            period = "2nd Semester";
        }else if (period == 'SUM') {
            period = "Summer";
        }

        var queryStr = "SELECT SubjectCode,Grade FROM dbo.grade_list_" + year + " WHERE StudentNo=\'" + id + "\' and Semester=\'" + period + "\'";

        console.log(queryStr);

        dataContext.query(queryStr, callback);

    }
}

index.js

var express = require('express');
var sql = require('../libs/sql');

var router = express.Router();

router.get('/', function(req, res, next) {

    sql.DataQuery("01-666-123/sem2/2009", function(err, data) {
        res.end(data);
    });

});

module.exports = router;
Sign up to request clarification or add additional context in comments.

1 Comment

The final call in your DataQuery function is a bit redundant. Just call dataContext.query(queryStr, callback).
1

The main problem with your code is that callbacks can be called at any time, specifically (usually) after the caller has returned. You need to complete the HTTP request in the callback from the sql provider:

var dataContext = require('node2sql')({
    username: 'user',
    password: 'password',
    server: '1xx.xx2.xx.xx9',
    options: {
        database: 'grades'
    }
});

module.exports = {
    DataQuery: function(query,fn) {
        query = query.replace(/\s+/g, '').split("/");
        year = query[2].substr(2, 4);
        id = query[0];
        period = query[1].toUpperCase();

        if (period == 'SEM1') {
            period = "1st Semester";
        } else if (period == "SEM2") {
            period = "2nd Semester";
        }else if (period == 'SUM') {
            period = "Summer";
        }

        var queryStr = "SELECT SubjectCode,Grade FROM dbo.grade_list_" + year + " WHERE StudentNo=\'" + id + "\' and Semester=\'" + period + "\'";

        console.log(queryStr);

        dataContext.query(queryStr, function(err, results) {
            console.log(results)
            // Note: Call code provided by your http server now that
            // the result is known.
            fn(err, results);
        });

    }
}

And in your http server:

var express = require('express');
var sql = require('../libs/sql');

var router = express.Router();

router.get('/', function(req, res, next) {

    // Note: The callback will fire when the sql data is available.
    // The function below goes into the fn argument in DataQuery.
    // DataQuery calls it when the data is ready.
    sql.DataQuery("01-666-123/sem2/2009", function(err, data) {
        res.end(data);
    });

});

module.exports = router;

1 Comment

Thanks for the answer @Art. I've tried both of your code and this fixed my problem.

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.