2017-06-22 55 views
0

我使用CLI将动作部署到OpenWhisk作为Web动作。我试图从邮差/卷曲中调用它。我回来,我想在身体参数的建议 hereOpenWhisk WebAction响应返回空体

这里的响应是我的代码:

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,直接在主函数内部的决心块?

回答

1

您的功能需要返回承诺,如return authenticateUser

+0

啊!这样一个小错误!谢谢你的帮助。 –