JS:forEach循环上的TypeError

时间:2017-11-13 16:07:07

标签: javascript node.js foreach

我从jwt抓取signature元素,然后调用post('/audit')并使用forEach循环中的签名。但是,forEach循环会引发错误:

  

TypeError:toAudit.forEach不是函数

导致错误的原因是什么?我该如何解决?

(server.js)

//retreives signature based on uuid without verifying asset. used for getting signatures for logs.
function getSignature(uuid){
    console.log('start getSignature');
    return new Promise(function (resolve, reject){
        request.post({
            url: `http://${ksiIP}:${ksiPort}/KsiProxy/rest/util/getSignature`, 
            headers: {
                'Content-Type': 'application/json',
                'Authorization' : 'Basic Og=='
            }, json: {'uuid': uuid}
        }, function(error, response, body){
            if(error || (response.statusCode != 200)){
                return reject((error ? error : body));
            }
            if(body.resultcode != 0){
                console.log("Error: " + requestResult.info);
                console.log("Asset could not be verified.");
                return reject(new Error(body.info));
            }
            //add identification as well
            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;
    }
    let toAudit = req.jwtPayload.signature 
    console.log('jwtPayload:', req.jwtPayload);
    console.log("ids: ", toAudit);

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

    toAudit.forEach( (element, index, array) => { //**error point HERE**
        getSignature(element.signature)
        .then((error, result) => {
            (result) => { array[index].signature = result };
            (error) => {
                    failed++;
                    array[index].signature = "unavailable";
                    console.log(error.message);
                }
    });
    });

0 个答案:

没有答案