Unexpected result when destructuring object

Viewed 39

For an Express project, I'm working with the Github API for implementing OAuth in my application. I have a _json object, returend like this;

{
  login: "basvandriel",
  id: 5286260,
  email: "contact@basvandriel.nl"
}

Ofcourse, the object has more data in it, but I reduced it for this example.

For accessing the data, I can do _json.email, or any other object key, which returns the data properly. The problem is however, when trying to destruct the object by trying the following code, It returns undefined.

passport.use(new GithubStrategy({
  clientID: GITHUB_CLIENT_ID,
  clientSecret: GITHUB_CLIENT_SECRET,
  callbackURL: "http://localhost:4000/auth/github/callback"
},
async function(request, accessToken, refreshToken, profile, done) {
  const {
    id,
    username,
    email
  } = profile._json;

  console.log(email) //undefined
  console.log(profile._json.email) // not undefined

  // ...

}));

This comes from using the passport-github2 package. I tried wrapping the _json object with Object(_json), but had no luck either.

For a quick fix, I'm just doing this

const email = profile._json.email;

Why isn't this working? Is there something I'm missing?

1 Answers

Can you try doing this:

const {
    id: id,
    username: username,
    email: email
  } = profile._json;
Related