Azure 函数 httptrigger 未从 SQL 服务器请求返回响应
Azure function httptrigger not returning response from SQL Server request
我正在尝试创建一个连接到 SQL 服务器的 Azure 函数。响应未被执行。我可以在日志中看到正确的数据 returned。关于请求完成后如何将 context.res 变为 return 的任何想法?
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();
};
请将您的代码更改为:
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();
}
};
您在设置 context.res
的值后缺少对 context.done()
的调用。没有 context.done()
函数的调用将超时。
我正在尝试创建一个连接到 SQL 服务器的 Azure 函数。响应未被执行。我可以在日志中看到正确的数据 returned。关于请求完成后如何将 context.res 变为 return 的任何想法?
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();
};
请将您的代码更改为:
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();
}
};
您在设置 context.res
的值后缺少对 context.done()
的调用。没有 context.done()
函数的调用将超时。