【发布时间】:2017-08-07 01:15:21
【问题描述】:
我在 Bluemix 上的 OpenWhisk 上创建了两个操作。当我可以从 OpenWhisk 平台外部调用它们时,它们都可以独立工作。但我想从 action2 中调用 action1,并使用以下语法:
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);
});
}
但是我得到一个空结果并且没有控制台消息。一些帮助会很棒。
更新1:
按提示添加return选项时,返回OpenWhisk的Promise,如下:
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的响应值不符合预期,但包含:
{ "isFulfilled": false, "isRejected": false }
我期望 action2 的返回消息(读取 Google Sheets API)并解析结果:
{
"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 外部单独测试 action2 时,或者直接通过 OpenWhisk 中的“运行此操作”/ Bluemix 它返回正确的值。
更新2:
好的解决了。我在 action1 中调用的函数中将 ow.actions.invoke 调用到 action2,这种返回嵌套导致了问题。当我直接在主函数中移动调用代码时,一切都按预期解决了。嵌套 promise 和 return 时的双重麻烦。过失。谢谢大家
【问题讨论】: