OpenWhisk WebAction 响应返回空体

OpenWhisk WebAction response returning empty body

我使用 CLI 作为 Web 操作将操作部署到 OpenWhisk。我正在尝试从 Postman/curl 调用它。我正在按照建议在 body 参数中返回我想要的响应

这是我的代码:

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 块?

您的函数需要 return return authenticateUser 中的承诺。