此代码指向用户输入用户名/密码的位置,然后返回令牌。如果他们输入错误的信息并获得401,我会把它拿到那里,他们再次得到提示再次使用.catch(function(err) { callApiToken(); })
。但是,在第二次传递中,我将获得Unhandled rejection TypeError: object is not a function
payload(user, pass)
函数。为什么是这样?
function payload(user, pass) {
return payload = {
"thePassword": {
"username": user,
"password": pass
}
}
}
function postAsync(url, payload) {
return new Promise(function(resolve, reject) {
rest.postJson(url, payload)
.on('fail', function(data, response) {
httpFail(data, response, reject);
reject(response.statusCode);
})
.on('error', function(err, response) {
conError(err, response);
reject(err);
})
.on('success', function(data, response) {
writeToFile(data, response);
resolve();
});
});
}
function getPassword(user) {
return readAsync({prompt: "Password: ", silent: true, replace: "*" })
.then(function(pass) {
//PAYLOAD() IS WHERE THE ERROR IS AT SECOND TIME
//BEING CALLED
return postAsync(URL, payload(user[0], pass[0]));
});
}
如何调用:
callApiToken()
.then(function() { refreshToken(); })
.catch(function(err) {
callApiToken();
});
答案 0 :(得分:2)
您的问题是有效负载功能本身,尤其是这部分:
return payload = {
"thePassword": {
"username": user,
"password": pass
}
}
这是做什么的,它将有效载荷设置为" thePassword"对象,因此在第一次调用之后,不再是函数而是对象。
请改为:
function payload(user, pass) {
return {
"thePassword": {
"username": user,
"password": pass
}
}
}