简体   繁体   English

解构 object 时出现意外结果

[英]Unexpected result when destructuring object

For an Express project, I'm working with the Github API for implementing OAuth in my application.对于 Express 项目,我正在使用 Github API 在我的应用程序中实现 OAuth。 I have a _json object, returend like this;我有一个_json object,像这样返回;

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

Ofcourse, the object has more data in it, but I reduced it for this example.当然,object 里面有更多的数据,但是我在这个例子中减少了它。

For accessing the data, I can do _json.email , or any other object key, which returns the data properly.为了访问数据,我可以执行_json.email或任何其他 object 键,它可以正确返回数据。 The problem is however, when trying to destruct the object by trying the following code, It returns undefined .然而,问题是,当尝试通过尝试以下代码来破坏 object 时,它返回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.这来自使用passport-github2 package。 I tried wrapping the _json object with Object(_json) , but had no luck either.我尝试用Object(_json)包装_json object ,但也没有运气。

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?有什么我想念的吗?

Can you try doing this:你可以尝试这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM