1

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;
};
15
  • 1
    Can you supply an example initialization of data for which it does not work? Commented Jan 23, 2017 at 21:23
  • Also, probably not related to your issue, but there's probably no need to return data from the function since it is editing the original object passed in by reference. Commented Jan 23, 2017 at 21:24
  • 1
    This function will work as intended. The problem is somewhere else. Commented Jan 23, 2017 at 21:25
  • 1
    Is this line a typo? if (data.emailAddressVerficationCode) delete data.emailAddressVerficationCode; Verfication instead of Verification. Other than that, it works fine for me in the Chrome console. Commented Jan 23, 2017 at 21:36
  • 1
    When I run the code, it removes the password property, so @MezoIstvan has it right. Commented Jan 23, 2017 at 21:39

2 Answers 2

3

You are probably using Mongoose or any other ODM, right? If so, you have to know that you can not change the results unless you call the method .lean() (http://mongoosejs.com/docs/api.html#query_Query-lean).

Mongoose keeps the model safe from any modifications unless you detach the result.

Sign up to request clarification or add additional context in comments.

3 Comments

This worked! Never knew this about Mongoose. Thank you! Thanks to @JohnnyHK too for leading me to this answer.
In fact, you should always use .lean() if you know for sure that you are not going to change the model resulting from the query; You´ll spend much less memory and increase performance as the objects will be much lighter. Another tip would be to use projections so you don´t get any values from the database that you are not going to use.
I will do from now on. Is there a way to use lean without having to use the .exec() syntax for the callback but rather the callback syntax that was used in my example?
-1

Remove the comma after the last curly bracket in your JSON.

1 Comment

This was a typo in the question. In the application the data is being fetched from MongoDB and will not have these errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.