Gaurang Deshpande
Gaurang Deshpande

Reputation: 232

OpenWhisk WebAction response returning empty body

I deployed an action to OpenWhisk using CLI as a web action. I am trying to invoke it from Postman/curl. I am returning the response that I want in body parameter as suggested here

Here is my code:

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;

Deploying the action as:

$ wsk -i action create /guest/package/actionName --kind nodejs:6 zipFileWithPackageJson.zip --web true

The situation is, when I use the if-else if loop without enclosing it in function authenticationUser(), I can get the response. When I enclose it in the function and try to invoke the function like I am doing it above, I get a blank response. I have to enclose it in a function because I have to perform a series of operations before I check the username and password. I will be enclosing these operations in different functions and invoke them one after another using

function1()
 .then(function2)
 .then(function3)
 .then(authenticateUser)
 .catch(err => {
    console.log(err)
  }); 

If I check the logs for this action using command $ wsk -i activation logs <activation ID> I can see them as expected. Just that the response body is totally empty.

Is it the syntax in NodeJS that I am writing incorrectly or is it the OpenWhisk that expects the resolve block inside the main function directly?

Upvotes: 0

Views: 238

Answers (1)

user6062970
user6062970

Reputation: 831

Your function needs to return the promise as in return authenticateUser.

Upvotes: 1

Related Questions