如何在 bluemix 上的 openwhisk 平台中调用 openwhisk 操作?
How to invoke openwhisk action within openwhisk platform on bluemix?
我在 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选项时,到returnOpenWhisk的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 的 return 消息(读取 Google 表格 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' 变量?因为当我单独测试 action2 时,从 OpenWhisk 外部通过 Postman 或 API 连接,或直接通过 'Run this action' 在 OpenWhisk/Bluemix 它 return 是正确的值。
更新2:
好的,解决了。我在 action1 中调用的函数中将 ow.actions.invoke 调用到 action2,这种 return 的嵌套导致了问题。当我直接在主函数中移动调用代码时,一切都按预期解决了。嵌套 promise 和 returns 时的双重麻烦。我认罪。谢谢大家
您需要 return 在函数中使用 Promise 试试这个
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;
});
}
如果您只想调用操作:
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'
};
}
如果您想等待调用操作的结果:
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
};
});
}
我在 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选项时,到returnOpenWhisk的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 的 return 消息(读取 Google 表格 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' 变量?因为当我单独测试 action2 时,从 OpenWhisk 外部通过 Postman 或 API 连接,或直接通过 'Run this action' 在 OpenWhisk/Bluemix 它 return 是正确的值。
更新2:
好的,解决了。我在 action1 中调用的函数中将 ow.actions.invoke 调用到 action2,这种 return 的嵌套导致了问题。当我直接在主函数中移动调用代码时,一切都按预期解决了。嵌套 promise 和 returns 时的双重麻烦。我认罪。谢谢大家
您需要 return 在函数中使用 Promise 试试这个
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;
});
}
如果您只想调用操作:
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'
};
}
如果您想等待调用操作的结果:
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
};
});
}