Node.js 'Object is not a function' 第二个 运行 的 payload()
Node.js 'Object is not a function' for payload() on second run
此代码用于用户输入 username/password 他们会取回令牌的地方。我知道如果他们输入错误的信息并收到 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();
});
您的问题是负载函数本身,尤其是这部分:
return payload = {
"thePassword": {
"username": user,
"password": pass
}
}
它的作用是将负载设置为 "thePassword" 对象,以便在第一次调用后不再是函数而是对象。
改为这样做:
function payload(user, pass) {
return {
"thePassword": {
"username": user,
"password": pass
}
}
}
此代码用于用户输入 username/password 他们会取回令牌的地方。我知道如果他们输入错误的信息并收到 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();
});
您的问题是负载函数本身,尤其是这部分:
return payload = {
"thePassword": {
"username": user,
"password": pass
}
}
它的作用是将负载设置为 "thePassword" 对象,以便在第一次调用后不再是函数而是对象。
改为这样做:
function payload(user, pass) {
return {
"thePassword": {
"username": user,
"password": pass
}
}
}