0

When I call /audit which then uses getSignature(uuid) function. In the getSignature function, the url is valid but when I call it I get the response that all of its body is null. Why is the url returning null signature and body while it's a valid url?

EDIT: Moving the respondWithJSON function into .then() promise statement of var getAudit, fixed the failed variable error. Error with getSignature still persists.


(received error):

[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

(expected error):

{
    "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;
    });
}); 
BenSmith
  • 429
  • 1
  • 6
  • 14
  • You may want to take a look at https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call -- essentially the code inside the `.then` callback for runs _after_ the if statements, `if(failed` etc. – Explosion Pills Nov 15 '17 at 18:31
  • Your suggestion fixed the variable failed, error. However, I still have the null body issue. – BenSmith Nov 15 '17 at 18:57

0 Answers0