我使用命令行界面将一个操作部署到OpenWhisk。我正在尝试从Postman/curl调用它。按照建议的 here ,我将在主体参数中返回我想要的响应
下面是我的代码:
function login(args){
const username = args.user.username;
const password = args.user.password;
const user = {
uName: 'abc',
userCn: 'Full Name',
token: 'fsgdfhj'
};
function authenticateUser(){
if(username === '' || password === ''){
return new Promise((resolve, reject) => {
reject({
headers: { 'Content-Type': 'application/json' },
statusCode: 401,
body: "Please enter username and password!"
});
});
}
else if(username==='a' && password ==='a'){
return new Promise((resolve, reject) => {
resolve({
headers: { 'Content-Type': 'application/json' },
statusCode: 200,
body : user
}) ;
});
}
}
authenticateUser()
.then(() => {
console.log('resolved and body is -> '+user);
})
.catch(()=> {
console.log('rejected -> '+stderr);
});
}
exports.main = login;将操作部署为:
$ wsk -i action create /guest/package/actionName --kind nodejs:6 zipFileWithPackageJson.zip --web true情况是,当我使用if-else if循环而不将其封装在function authenticationUser()中时,我可以得到响应。当我把它封装在函数中并尝试像上面那样调用函数时,我得到了一个空白响应。我必须将其封装在一个函数中,因为在检查用户名和密码之前,我必须执行一系列操作。我将把这些操作封装在不同的函数中,并一个接一个地使用
function1()
.then(function2)
.then(function3)
.then(authenticateUser)
.catch(err => {
console.log(err)
}); 如果我使用命令$ wsk -i activation logs <activation ID>检查此操作的日志,我可以按预期看到它们。只是响应体是完全空的。
是我写错了NodeJS中的语法,还是OpenWhisk直接期望在main函数中使用resolve块?
发布于 2017-06-23 04:46:57
您的函数需要像在return authenticateUser中一样返回promise。
https://stackoverflow.com/questions/44708190
复制相似问题