使用url返回null body的Promise函数,同时使用有效的url

时间:2017-11-15 18:27:38

标签: javascript node.js database

当我致电/audit然后使用getSignature(uuid)功能时。在getSignature函数中,url是有效的,但是当我调用它时,我得到的响应是它的所有正文都是空的。为什么url返回空签名和正文,而它是一个有效的网址?

编辑:将respondWithJSON函数移动到var getAudit的.then()promise语句中,修复了failed变量错误。 getSignature的错误仍然存​​在。

(收到错误):

[stdout] start getSignature
[stdout] start responseWithJSON
[stdout] error in getSig: null
[stdout] uuid: 9a09d117-56e9-4200-be42-185eb19a2a50
[stdout] url http://url/KsiProxy/getSignature
[stdout] error1
[stdout] error4
[stdout] { signature: null,
[stdout]   resultcode: 1,
[stdout]   info: null,
[stdout]   signGw: null,
[stdout]   extGw: null,
[stdout]   pubGw: null,
[stdout]   catena: null,
[stdout]   identity: null }
[stdout] getAudit: Promise { <pending> }
[stdout] toAudit: unavailable

(预期错误):

{
    "signature": "iAAGkYgBAGkCBFoKAX0DBCbO0Wk...",
    "resultcode": 0,
    "info": null,
    "signGw": null,
    "extGw": null,
    "pubGw": null,
    "catena": null,
    "identity": "North-America.abc.abc.abc"
}

(server.js)

function getSignature(uuid){
    console.log('start getSignature');
     return new Promise(function (resolve, reject){
        request.post({
            url: `http://${ksiIP}:${ksiPort}/KsiProxy/getSignature`, //**HERE
            headers: {
                'Content-Type': 'application/json',
                'Authorization' : 'Basic Og=='
            }, json: {'uuid': uuid}
        }, function(error, response, body){
            console.log('error in getSig:', error);
            console.log('uuid:', uuid);
            console.log('url', `http://${ksiIP}:${ksiPort}/KsiProxy/getSignature`);
            if(error || (response.statusCode != 200)){
                console.log('error1');
                return reject((error ? error : body));
            }
            if(body.resultcode != 0){
                console.log('error2');
                console.log("Error: " + requestResult.info);
                console.log("Asset could not be verified.");
                return reject(new Error(body.info));
            }
            console.log('body.signature', body.signature);
            resolve(body.signature);

        }).auth('','',true);

     });
}


dcServer.post('/audit', function(req,res){ 
    console.log('--- start /audit'); 
    if(req.jwtPayload.description != "admin"){
        respondWithJSON(401, 401, "Not authorised for this functionality.", res);
        return;
    }
    var toAudit = req.jwtPayload.signature; 

    console.log("ids: ", toAudit);

    var failed = 0;//counter to increase if signatures couldnt be retreived. 

    var getAudit = getSignature(toAudit).then( //**HERE
                (result) => { console.log('error 3');
                                return toAudit = result;
                            } ,
                (error) => {
                        console.log('error4');
                        failed++;
                        console.log(error);
                        return toAudit = 'unavailable';
        }).then(() => {
            if(failed > 0){
            respondWithJSON(200,1, `${failed}, signature(s) could not be retrieved.`, res, toAudit);
            }else{
            respondWithJSON(200,0, `${failed} failed amount. Success`, res, toAudit);
            }
            console.log('getAudit:', getAudit);
            console.log('toAudit:', toAudit);
            return toAudit;
    });
}); 

0 个答案:

没有答案