这是我的终点
router.post("/users/github/:code", function(req, res) {
var state;
fetch('https://github.com/login/oauth/access_token/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'xxx',
client_secret: 'xxx',
code: req.params.code
})
}).then(function(res) {
return res.json();
}).then(function(body) {
if (body.error !== 'bad_verification_code') {
const token = createToken(body.access_token);
var access_token = body.access_token;
fetch('https://api.github.com/orgs/xxx/members?access_token=' + access_token, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}
}).then(res => res.json())
.then(res => {
fetch('https://api.github.com/user?access_token=' + access_token, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
}).then(res => res.json())
.then(user => {
var username = user.login;
var insert;
for (insert = 0; insert < res.length; insert++) {
if (username == res[insert].login) {
state = true;
}
};
console.log(state)
if (state) {
res.json({
success: true,
access_token: body.access_token,
jwt: token
});
} else {
res.json({
success: false
});
}
});
});
} else {
res.json({
success: false
});
}
});
});
这是引发错误的部分
if(state){
res.json({success: true , access_token: body.access_token, jwt: token});
}else{
res.json({success: false});
}
为什么会发生这种情况?
发生的情况是您正在调用
res
2+ 个相同的参数,其中一个express
和您创建的对象之一,它们被重新声明,您必须为每个参数指定不同的名称,fetch
以便参数可以使用方法,即:res
express
json
只是为了增加对伤害的侮辱,我认为你错过了承诺链的意义。Jorius 的回答可以改写为: