获取回调结果(Nodejs)

时间:2017-02-08 04:35:51

标签: javascript node.js express callback tedious

我的express和sql查询代码有问题。我无法获得下面的SQL查询results的值,但这在记录results时有效。我已经在这里返回results

这是我的sql.js代码

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;
        });

    }
}

这是我的索引页面,您可以看到我想获得data的返回值并将其显示到我的索引页面(仅限测试)。

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;

我接受你的任何建议。我已经阅读了之前被问过的question,但我仍然无法弄清楚如何使这项工作。感谢

2 个答案:

答案 0 :(得分:2)

您无法从回调中返回。 (回调是异步的,可以随时完成。)试试这个:

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;

答案 1 :(得分:1)

您的代码的主要问题是可以随时调用回调,特别是(通常)在调用者返回后调用。您需要在sql提供程序的回调中完成HTTP请求:

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);
        });

    }
}

在您的http服务器中:

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;