Javascript 解构对象时出现意外结果
对于一个Express项目,我正在使用GithubAPI在我的应用程序中实现OAuth。我有一个Javascript 解构对象时出现意外结果,javascript,express,destructuring,Javascript,Express,Destructuring,对于一个Express项目,我正在使用GithubAPI在我的应用程序中实现OAuth。我有一个\uJSON对象,如下所示 { login: "basvandriel", id: 5286260, email: "contact@basvandriel.nl" } 当然,对象中有更多的数据,但在本例中我减少了它 为了访问数据,我可以执行\u json.email,或任何其他正确返回数据的对象键。但是,问题是,当尝试通过尝试以下代码来破坏对象时
\uJSON
对象,如下所示
{
login: "basvandriel",
id: 5286260,
email: "contact@basvandriel.nl"
}
当然,对象中有更多的数据,但在本例中我减少了它
为了访问数据,我可以执行\u json.email
,或任何其他正确返回数据的对象键。但是,问题是,当尝试通过尝试以下代码来破坏对象时,它返回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
// ...
}));
这来自于使用passport-github2
包。我尝试用对象(\u json)
包装\u json
对象,但也没有成功
为了快速解决问题,我只是这么做
const email = profile._json.email;
为什么这不起作用?我有什么遗漏吗?你能试着这样做吗:
const{
id:id,
用户名:用户名,
电邮:电邮
}=profile.\u json;
为什么这会改变什么?它起作用了,但为什么呢?@Bas,如果我说实话,我也不知道。我们应该等待对这件事有了解的人,我很好奇为什么它也能起作用,@Bas我想你的意思是登录而不是用户名