2017-03-16 57 views
0

我在Bluemix上的OpenWhisk上创建了两个操作。当我可以从OpenWhisk平台之外打电话时,两者都独立工作。但我想从1动作中调用动作1,和现在用的语法如下:如何在bluemix上的openwhisk平台内调用openwhisk操作?

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

但我得到一个空的结果,并没有控制台消息。一些帮助会很好。

UPDATE1:

当添加的建议返回选项,返回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; 
}); 

动作2的响应值并不如预期,但包含:

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

其中我期待action2(读取Google Sheets API)的返回消息并分析结果:

{ 
    "duration": 139, 
    "name": "getEventCfps", 
    "subject": "[email protected]", 
    ... 
    "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" 
    }, 
    ... 
} 

所以我期待我不解析'.then(结果'变量在action1中正确?当我从OpenWhisk通过Postman或API Connect单独测试action2时,或者直接通过OpenWhisk/Bluemix中的“运行此操作”,它会返回正确的值。

UPDATE2:

好的解决。我在一个在action1中调用的函数中调用了ow.actions.invoke到action2,这个返回嵌套导致了这个问题。直接在主函数中移动调用代码时,所有解决方案都按预期方式解决。嵌套承诺和返回时遇到双重麻烦。 Mea culpa。谢谢大家

回答

4

你需要在你的函数返回一个承诺试试这个

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

注意'openwhisk'已经返回一个承诺,因此,创建一个新的是多余的。你可以'返回ow.actions.invoke(...)' – markusthoemmes

+0

我已经在编辑中解决了这个问题。 –

+0

该文档不是很清楚,如果它是actionName或name,或者两者中的任何一个都可以使用。 – csantanapr

相关问题