Azure Functions,http请求返回第二个值

时间:2017-01-31 17:53:08

标签: node.js function http azure

我正在使用TypeScript创建一个Node.js API,我想在Azure Functions中托管它。它将请求发送到另一个公共API。我得到了它的工作,但它有一个我无法解决的错误。 我正在使用Postman向我的API发送http post请求。在请求的主体中有一个JSON对象,其中包含一个属性" id"。

但我不能将最后一个值作为回复。我得到第二个最后一个值作为回应。假设第一个请求带有id" 1"得到好的"作为回应。带有id" 2"的第二个请求得到一个好的"作为回应。但它应该得到“坏”"作为回应。带有id" 3"的第三个请求得到一个"坏"作为回应但它应该得到好的"。带有id" 4"的第四个请求得到好的"作为回应,但它应该得到"坏"。

我的API中存在问题。当我通过Postman直接向另一个API发送请求时,我不会遇到这个问题。

index.js

module.exports = function (context, req) {

if (req.body.checkID) {
    api.validationStatus(req.body.checkID)
    .then(function (resp) {
        if (resp.validated) { // Number is correct
            context.log('Result: good');       
            res = { body: "Result: good" }              
        }
        else if (resp.name === "StatusCodeError") { // Number is not correct
            context.log('Result: bad ' + resp.name);
            res = { body: "Result: bad " + resp.name }   
        }
        else if (resp.name === "RequestError") { // API is not reachable
            context.log('Result, bad ' + resp.name);
            res = { body: "Result, bad " + resp.name }  
        }
        else { // Something else went wrong
            context.log("Result, bad.");
            res = { body: "Result: bad." }  
        }
    }); 
}
else {
    res = {
        status: 400,
        body: "Please pass an id"
    };
}

context.done(null, res);};

api.js

myApi.prototype.validationStatus = function (id) {
    // Request options
    var options = {
        method: 'get',
        url: this.config.validationStatusLocationUrl.replace("{id}", id),
        headers: { "Authorization": this.config.apiKey }
    };
    // Request with a promise
    return reqpromise(options)
        .then(function (res) {
        return JSON.parse(res);
    })
        .catch(function (err) {
        return err;
    });
};

1 个答案:

答案 0 :(得分:0)

我明白了。解决方案很简单,我想我知道为什么会有这种行为。 context.done();需要在promise的.then-Section中执行(在if和else部分中)。问题是,即使当前的api请求(api.validationStatus())尚未完成(异步,循环风格),我也发送了响应,因此我得到了之前请求的旧值。

index.js

module.exports = function (context, req) {

    if (req.body.checkID) {

        api.validationStatus(req.body.checkID)
        .then(function (resp) {
            if (resp.validated) { // Number is correct
                context.log('Result, good.');       
                context.res = { body: "Result, good." }    
            }
            else if (resp.name === "StatusCodeError") { // Number is not correct
                context.log('Result, bad: ' + resp.name);
                context.res = { body: "Result, bad." + resp.name }   
                context.done();   
            }
            else if (resp.name === "RequestError") { // API is not reachable
                context.log('Result, bad: ' + resp.name);
                context.res = { body: "Result, bad." + resp.name }
                context.done();   
            }
            else { // Something else went wrong
                context.log("Result, bad.");
                context.res = { body: "Result, bad." }  
                context.done();   
            }
            context.done();
        }); 
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass an id"
        };
        context.done();
    }
};