This is a function that removes sensitive information from a JSON object before it gets returned to the client. The data that's being passed into the function would either be a JSON object or an array of JSON objects. Why would this function not work?
I know that there are other solutions to the problem, but this is annoying my brain.
I have logged plenty of information in this function and even though JavaScript is asynchronous the functions are running in the order that they should - the recursion is finishing before the final return statement is hit.
The issue right now is that even though everything seems to be working and the delete operator is returning true, the attributes being deleted are still present when the function finally returns.
Example data which is being fetched from MongoDB:
[
{
'id': '1',
'name': 'John',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': 'A897D'
},
{
'id': '2',
'name': 'Andrew',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': '90H8D'
},
{
'id': '3',
'name': 'Matthew',
'password': 'test123',
'emailAddress': '[email protected]',
'emailAddressVerificationCode': '56C7C'
}
]
Any thoughts would be appreciated.
UserService.cleanJSON = (data) => {
if (Array.isArray(data)) {
for (let i = 0; i < data.length; i++){
data[i] = UserService.cleanJSON(data[i]);
}
} else {
if (data.password) delete data.password;
if (data.emailAddressVerficationCode) delete data.emailAddressVerficationCode;
if (data.mobileNumberVerificationCode) delete data.mobileNumberVerificationCode;
if (data.accountType) delete data.accountType;
}
return data;
};
datafor which it does not work?probablynot related to your issue, but there's probably no need to returndatafrom the function since it is editing the original object passed in by reference.if (data.emailAddressVerficationCode) delete data.emailAddressVerficationCode;Verfication instead of Verification. Other than that, it works fine for me in the Chrome console.passwordproperty, so @MezoIstvan has it right.