1

I fetch user Result from database(Mongodb using Mongoose) in Nodejs. Now I want to delete some keys in the user Result. But unable to delete its property. I searched many times but didn't find any solution to delete

Example:-

File1.js Fetched user by mongo query

userModel.findOne(query, projection)
      .then(function (result) {
          if (result)
              return result;
          return false
      })

The above query fetch the result from the database and the result looks like

user = {
 f_name: "Vikas",
 l_name: "Kohli",
 key1: "abcD",
 key2: "dhdhdh"

}

File2.js

user = {
     f_name: "Vikas",
     l_name: "Kohli",
     key1: "abcD",
     key2: "dhdhdh"

    }
 inputuser = "dfdfhdfgy"

Create userkey with using key1 of user result then compare

(_.isEqual(userkey, user.key2)){
 //call to next file
}else{
//send response as key not matched
}

Doing some logic, fetch the user's key and then doing some encryption method using key2

Finally compare key2 with userResullt key 2(that I fetched from db) If its ok, then pass to next function

File3.js Expected Output:-

user = {
         f_name: "Vikas",
         l_name: "Kohli"
        }

Current Scenario:-

user = {
         f_name: "Vikas",
         l_name: "Kohli",
         key1: "abcD"
         key2: "dhdhdh"

        }

Now want to send the response with the user result with deleted key1 and key2 but unable to delete that keys

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype. (From w3schools, developer.mozilla)

Also found some useful links but none of them working for me. How can I achieve that?

Clarification on the inability of javascript deleting inherited properties.

Why delete operator returns true even if property wasn't deleted from an object

4
  • 1
    too long post for something simple. Isn't delete key working? Commented Jun 18, 2018 at 7:35
  • not at all. Even it showing true but unable to delete that key as this object is inherited Commented Jun 18, 2018 at 7:36
  • You can delete/override the own properties of an object with the same name, but not inherited properties, unless you want to remove them from the prototype itself. A usual workaround is to make hasOwnProperty check when referring properties. Alternatively you can make a deep copy of the object, and include only the properties you need. Commented Jun 18, 2018 at 7:39
  • The question lacks stackoverflow.com/help/mcve . There should be some code that explains how exactly these objects are produced and how comes delete doesn't work. The question doesn't explain this, and no inheritance is shown. Commented Jun 18, 2018 at 7:46

3 Answers 3

1

There is a syntax error of comma in user object, correct it and apply delete operator

user = {
 f_name: "Vikas",
 l_name: "Kohli",
 key1: "abcD",
 key2: "dhdhdh"
}

delete user.key1
delete user.key2
Sign up to request clarification or add additional context in comments.

1 Comment

There is no syntax error. By mistake, I wrote a syntax error here. Now I changed
0

You can create a new object from the existing one, and just take the properties you need:

let user = {
    f_name: "Vikas",
    l_name: "Kohli",
    key1: "abcD",
    key2: "dhdhdh"
}

user = (({f_name, l_name}) => ({f_name, l_name}))(user);

console.log(user);

If you have many properties to keep, and only few to reject, then you can list the properties to reject like this:

let user = {
    f_name: "Vikas",
    l_name: "Kohli",
    key1: "abcD",
    key2: "dhdhdh"
}

const reject = new Set(["key1", "key2"]);
user = Object.assign(...Object.entries(user)
                              .filter(([k,v]) => !reject.has(k))
                              .map(([k,v]) => ({[k]: v})));

console.log(user);

4 Comments

Does this answer your question? Could you leave some feed-back?
If there was 100 properties,then it's not good to write 100 properties that I want to send
So how many properties do you have? How many properties do you need to delete?
Anyway, I added an alternative, where you need to specify the list of keys to omit instead of those to keep.
0

delete will not work on model returned by mongoose, but can be deleted after converting to object.

user = user.toObject();
delete user.key1;
delete user.key2;

Comments

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.