在我的自定义Azure API中,我想将请求作为参数传递给函数。从该函数我希望能够访问查询参数,并使用service.mssql在我的数据库上执行查询。但是request.query.fieldName在函数中是null。
exports.post = function(request, response) {
console.log("passed UserID = " + request.query.UserID);
request.service.mssql.query(query, {
success: function(results) {
if (foo) {
callFunction(results[0], request);
}
});
function callFunction(firstResult, request) {
console.log("UserID received in function = " + request.query.UserID);
}
在我的控制台中,我看到:
Error Error in script '/api/testapi.js'. TypeError: Cannot read property 'UserID' of undefined
请求有什么特别之处,这意味着它无法传递给新函数吗?
答案 0 :(得分:0)
我很乐意看到这方面的一些文档,但似乎变量请求和响应必须是传递给函数的前两个,如果它们能够正常工作的话。这段代码可以使用:
exports.post = function(request, response) {
console.log("passed UserID = " + request.query.UserID);
request.service.mssql.query(query, {
success: function(results) {
if (foo) {
callFunction(request, response, results[0]);
}
});
function callFunction(request, response, firstResult) {
console.log("UserID received in function = " + request.query.UserID);
}