我使用CLI将操作部署到OpenWhisk作为Web操作。我试图从Postman / curl调用它。我按照建议在body参数中返回我想要的响应 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函数中使用解析块?
答案 0 :(得分:1)
您的函数需要返回Object
中的承诺。