0

I have created two actions on OpenWhisk on Bluemix. Both independently work fine when I can call them from outside the OpenWhisk platform. But I want to call action1 from within action2, and am using the following syntax:

var openwhisk = require('openwhisk');
function main(args){
  const name = 'action2';
  const blocking = true;
  const params = { param1: 'sthing'};
  var ow = openwhisk();
  ow.actions.invoke({name, blocking, params})
  .then(result => {
    console.log('result: ', result);
    return result; // ?
  }).catch(err => {
    console.error('failed to invoke actions', err);
  });
}

But I get an empty result and no console messages. Some help would be great.

Update1:

When adding as suggested the return option, to return the Promise of OpenWhisk, as follows:

return ow.actions.invoke({name, blocking, params})
.then(result => {
  console.log('result: ', result);
  return result;
}).catch(err => {
  console.error('failed to invoke actions', err);
  throw err;
});

the response value of action2 is not as expected but contains:

{ "isFulfilled": false, "isRejected": false }

where I expect the return message of action2 (which reads a Google Sheets API) and parses the result:

{
  "duration": 139,
  "name": "getEventCfps",
  "subject": "me@email.com",
  ...
  "response": {
    "result": {
      "message": [
        {
          "location": "Atlanta, GA",
          "url": "https://werise.tech/",
          "event": "We RISE Women in Tech Conference",
          "cfp-deadline": "3/31/2017",
          ...
        }
      ]
    },
    "success": true,
    "status": "success"
  },
  ...
}

So I am expecting I am not parsing the '.then(result' variable in action1 correctly? cause when I test action2 separately, from outside OpenWhisk via Postman or API Connect, or directly by 'Run this action' in OpenWhisk/Bluemix it returns the correct values.

Update2:

Alright solved. I was calling the ow.actions.invoke to action2 in a function that was called within the action1, this nesting of returns, caused the issue. When I moved the invoke code directly in the main function, all resolved as expected. Double trouble when nesting promises and returns. Mea culpa. Thanks everyone

remkohdev
  • 250
  • 3
  • 13

2 Answers2

4

You need to return a Promise in your function try this

var openwhisk = require('openwhisk');
function main(args){
  const name = '/whisk.system/utils/echo';
  const blocking = true;
  const params = { param1: 'sthing'};
  var ow = openwhisk();

  return ow.actions.invoke({name, blocking, params})
  .then(result => {
    console.log('result: ', result);
    return result;
  }).catch(err => {
    console.error('failed to invoke actions', err);
    throw err;
  });
}
csantanapr
  • 5,002
  • 2
  • 19
  • 15
  • Note that `openwhisk` already returns a Promise, so creating a new one is redundant. You can just `return ow.actions.invoke(...)` – markusthoemmes Mar 16 '17 at 08:07
  • The docs are not very clear if it's actionName or name, or any of the two can be use. – csantanapr Mar 16 '17 at 12:32
  • So, I had actually tried this before and when I do it now, it returns 'something', but I dont understand the return value, so I got confused...the 'result' response value is { "isFulfilled": false, "isRejected": false } while i am expecting something like { "duration": 139, "name": "getEventCfps", "subject": "me@email.com", "version": "0.0.27", "response": { "result": { "message": [ { "location": "Atlanta, GA", – remkohdev Mar 16 '17 at 12:47
  • project the response: `result.response.result` – user6062970 Mar 16 '17 at 15:14
  • what does it mean to 'project the response'? – remkohdev Mar 16 '17 at 16:31
  • ps, within the '.then(result' the response value of action2 is present and correct, but it does not get passed correct back to the original calling client. – remkohdev Mar 16 '17 at 16:36
  • Ah never mind I solved it. Problem was that I had placed the method in a function, which was causing the unexpected response. Wrong handling of nested returns. Mea culpa. Thanks everyone. – remkohdev Mar 16 '17 at 16:41
0

If you just want to invoke the action:

var openwhisk = require('openwhisk');
function main(args) {

  var ow = openwhisk();
  const name = args.action;
  const blocking = false
  const result = false
  const params = args;

  ow.actions.invoke({
    name,
    blocking,
    result,
    params
  });

  return {
    statusCode: 200,
    body: 'Action ' + name + ' invoked successfully'
  };
}

If you want to wait for the result of the invoked action:

var openwhisk = require('openwhisk');
function main(args) {

  var ow = openwhisk();
  const name = args.action;
  const blocking = false
  const result = false
  const params = args;

  return ow.actions.invoke({
    name,
    blocking,
    result,
    params
  }).then(function (res) {
        return {
           statusCode: 200,
            body: res
        };
    });
}