对于Express项目,我正在使用Github API在我的应用程序中实现OAuth。我有一个_json对象,像这样返回;
{
login: "basvandriel",
id: 5286260,
email: "contact@basvandriel.nl"
}当然,对象中有更多的数据,但我在本例中减少了它。
为了访问数据,我可以使用_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包。我尝试过用Object(_json)包装_json对象,但也没有成功。
为了快速解决这个问题,我只是这样做
const email = profile._json.email;为什么这不起作用?我是不是漏掉了什么?
发布于 2021-06-03 03:41:29
你能试着这样做吗:
const {
id: id,
username: username,
email: email
} = profile._json;https://stackoverflow.com/questions/67811325
复制相似问题