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

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
  • 1
    too long post for something simple. Isn't `delete key` working? – Adelin Jun 18 '18 at 07:35
  • not at all. Even it showing true but unable to delete that key as this object is inherited – VIKAS KOHLI Jun 18 '18 at 07: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. – Teemu Jun 18 '18 at 07:39
  • The question lacks https://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. – Estus Flask Jun 18 '18 at 07:46

3 Answers3

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
dhaker
  • 1,605
  • 18
  • 19
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);
trincot
  • 317,000
  • 35
  • 244
  • 286
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;
Arjun Kava
  • 5,303
  • 3
  • 20
  • 20