0

我正在尝试创建一个连接到 SQL Server 的 Azure 函数。响应没有被执行。我可以看到日志中返回的正确数据。关于在请求完成后如何让 context.res 返回的任何想法?

var rows = [];

module.exports = function (context, req, res) {
context.log('JavaScript HTTP trigger function processed a request.');

if(req.query.nfcID) {
     connection.on('connect', function(err) {
         request = new Request("select tagUID, ProductID, Active From Tags where TagUID = "+ req.query.nfcID,
    function(err, rowCount, rows) {
    if (err) {
       context.log(err);
    } else {

        connection.close();
        context.log('rows: '+rowCount);

    }
});

request.on('row', function(columns) {
    var row = {};
    columns.forEach(function(column) {
        row[column.metadata.colName] = column.value;
    });
    rows.push(row);

    context.log(rows);

    context.res = {
            body: rows;
        }
});

connection.execSql(request);
});

}
else {
    context.res = {
        status: 400,
        body: "Please provide the nfc id"
    };
}
context.done();
};
4

2 回答 2

2

context.done()在设置 的值后,您错过了对的调用context.res。没有context.done()函数的调用就会超时。

于 2017-08-24T06:22:18.393 回答
2

请将您的代码更改为:

var rows = [];

module.exports = function(context, req, res) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.nfcID) {
        connection.on('connect', function(err) {
            request = new Request("select tagUID, ProductID, Active From Tags where TagUID = " + req.query.nfcID, function(err, rowCount, rows) {
                if (err) {
                    context.log(err);
                } else {

                    connection.close();
                    context.log('rows: ' + rowCount);

                }
            });

            request.on('row', function(columns) {
                var row = {};
                columns.forEach(function(column) {
                    row[column.metadata.colName] = column.value;
                });
                rows.push(row);

                context.log(rows);

                context.res = {
                    body: JSON.stringify(rows);
                }

                context.done();
            });

            connection.execSql(request);
        });

    } else {
        context.res = {
            status: 400,
            body: "Please provide the nfc id"
        };

        context.done();
    }


};
于 2017-08-24T05:59:19.150 回答