0

I have a snippet that queries a database and returns a collection of user objects:

users: () => User
      .find()
      .then(users => users.map(user => user._doc))
      .catch((err) => { throw err; }),

As apparent, each user object consists of fields, such as username, firstName, password, etc. However, I'd like to assign null to the password field before returning the object (user._doc). For this, I tried the following:

.then(users => users.map(user => { ...user._doc, password: null }))

But it failed with a syntax error. Any help?

Example data:

{
  "data": {
    "users": [
      {
        "_id": "5c8664161b81d506e5c98e98",
        "firstName": "Bill",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$m57TfbtPVGS5xnJ6PORO9A$G4cvgac/wdtnra0+st+VWTplFNL+6B5zCrGovdWh7jOGAc2el9kVRg",
        "username": "gates",
        "apiKey": "5c8664161b81d506e5c98e97",
        "privileges": [
          "GUEST",
          "AUTHOR",
          "EDITOR"
        ]
      },
      {
        "_id": "5c8666e132dfae0802bb855b",
        "firstName": "Jim",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$TGImmObOvhKN4L/gYHMnsg$6ymjG5iwXCgKkm5VL+jJs5qyqDKsMpSL3oJbg4sJaDHk1z+v1jtVQA",
        "username": "jimm",
        "apiKey": "5c8666e132dfae0802bb855a",
        "privileges": [
          "GUEST",
          "AUTHOR",
          "EDITOR"
        ]
      },
      {
        "_id": "5c873f9fd925640b0460e23b",
        "firstName": "Tom",
        "password": "$argon2i$v=19$m=4096,t=3,p=1$IHIUzO7eFjzVSxJa1aeIsQ$Z2LkCnBZCSutTTq0WLmF8KB8p/HJsvuPP2JnaVwzoYLyVh9/9bh6EA",
        "username": "tom",
        "apiKey": "5c873f9fd925640b0460e23a",
        "privileges": [
          "OWNER"
        ]
      }
    ]
  }
}
TheLearner
  • 2,813
  • 5
  • 46
  • 94

0 Answers0