I am getting ERR_HTTP_HEADERS_SENT cause of this issue. What is wrong with this code?
.then((userDetails: any) => {
userDetails.role.forEach((singleRole) => {
if (singleRole.name === 'admin') {
console.log('here')
userDetails.userToken = userToken;
return res.status(200).json({
status: 'OK',
resultData: userDetails,
});
}
});
user.userToken = userToken;
logger.info('Logged in successfully....');
return res.status(200).json({
status: 'OK',
resultData: user,
});
})
you can use the following code with for loop, it will wait for response because its not async like forEach
.then((userDetails: any) => {
for(const singleRole of userDetails.role) {
if (singleRole.name === 'admin') {
console.log('here')
userDetails.userToken = userToken;
return res.status(200).json({
status: 'OK',
resultData: userDetails,
});
}
});
user.userToken = userToken;
logger.info('Logged in successfully....');
return res.status(200).json({
status: 'OK',
resultData: user,
});
})