【问题标题】:OpenWhisk WebAction response returning empty bodyOpenWhisk WebAction 响应返回空正文
【发布时间】:2017-06-23 04:46:56
【问题描述】:

我使用 CLI 作为 Web 操作将操作部署到 OpenWhisk。我正在尝试从 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 函数中期望解析块?

【问题讨论】:

    标签: node.js ibm-cloud openwhisk


    【解决方案1】:

    你的函数需要像return authenticateUser一样返回promise。

    【讨论】:

    • 啊!这么小的错误!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多