1

我的 express 和 sql 查询代码有问题。我无法获得results下面 sql 查询的值,但这在记录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;

我接受你的任何建议。我已经阅读了之前提出的这个问题,但我仍然无法弄清楚如何进行这项工作。谢谢

4

2 回答 2

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;
于 2017-02-08T05:14:24.420 回答
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;
于 2017-02-08T05:23:19.133 回答