如何在bluemix上的openwhisk平台内调用openwhisk动作? [英] How to invoke openwhisk action within openwhisk platform on bluemix?

查看:101
本文介绍了如何在bluemix上的openwhisk平台内调用openwhisk动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Bluemix的OpenWhisk上创建了两个动作.当我可以从OpenWhisk平台外部调用它们时,两者都可以正常工作.但是我想从action2内调用action1,并且正在使用以下语法:

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.

更新1:

按建议添加返回选项时,如下所示返回OpenWhisk的承诺:

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;
});

action2的响应值不符合预期,但包含:

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

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

我希望在其中action2的返回消息(读取Google Sheets API)并解析结果:

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"
  },
  ...
}

因此,我希望我无法正确解析action1中的'.then(result'变量?"原因是当我分别通过Postman或API Connect从OpenWhisk外部或通过OpenWhisk/中的运行此操作"单独测试action2时引起的Bluemix它返回正确的值.

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:

好了.我在action1内调用的函数中调用ow.actions.invoke到action2,这种返回嵌套导致了问题.当我将调用代码直接移到主函数中时,所有解析均按预期进行.嵌套承诺和回报时会遇到双重麻烦. Mea culpa.谢谢大家

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

推荐答案

您需要在函数中返回Promise

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;
  });
}

这篇关于如何在bluemix上的openwhisk平台内调用openwhisk动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆