0

In resolver I use lodash method 'find', if I code as:

user: (_, { _id }) => find(users, _id ),

I get null as result while should not, it happens that I must destructure again:

user: (_, { _id }) => find(users, { _id }),

Why is this? I thought that destructuring happens once and then the named argument is passed into the function, how to understand it?

Ariam1
  • 1,673
  • 2
  • 13
  • 32

1 Answers1

1

The latter is not destructuring but rather just shorthand syntax for object initialization.

This

find(users, { _id })

is equivalent to

find(users, { _id: _id })

it's just more succinct. In both cases, you are creating an object with a property named _id and setting the value of that property to an existing variable, which happens to also be named _id. If the property and variable names match, the shorthand syntax can be used.

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183