ingo
ingo

Reputation: 856

Additional Parameter in array callback

I try to log a foreach callback to a file inside my node app. I try to use:

    var logger = fs.createWriteStream('tests/temp.js', {
        flags: 'a' 
    })

    req.body.selection.forEach(myFunction(logger));

    logger.end() // close string

In the callback I try:

function myFunction(mylogger,postvalue) {
    Object.entries(myJsonData.testCases).forEach(([key, value]) => {
        if(postvalue == key){
            console.log("gefunden");
            mylogger.write('some data') // append string to your file
        }
    });
}

But this is wrong. Anyone can help me t find the brainbreak?

Upvotes: 0

Views: 15

Answers (1)

bereal
bereal

Reputation: 34282

I suggest passing an arrow function expression which propagates both the logger and the callback argument to the destination:

req.body.selection.forEach((value) => myFunction(logger, value));

Upvotes: 2

Related Questions