`const noflo = require('noflo');
exports.getComponent = () => {
const c = new noflo.Component();
c.description = `This component calls a given callback function for each
IP it receives. The Callback component is typically used to connect
NoFlo with external Node.js code.`;
c.icon = 'sign-out';
c.inPorts.add('in', {
description: 'Object passed as argument of the callback',
datatype: 'all',
});
c.inPorts.add('callback', {
description: 'Callback to invoke',
datatype: 'function',
control: true,
required: true,
});
c.outPorts.add(
'error',
{ datatype: 'object' },
);
return c.process((input, output) => {
if (!input.hasData('callback', 'in')) {
return;
}
const [callback, data] = input.getData('callback', 'in');
console.log('callback', callback);
if (typeof callback !== 'function') {
output.done(new Error('The provided callback must be a function'));
return;
}
// Call the callback when receiving data
try {
callback(data);
} catch (e) {
output.done(e);
return;
}
output.done();
});
};
〜`我有一个简单的图形,其中包含来自noflo-core的三个组件,Callback.js和两个Output.js组件。 Output.js的out端口连接到Callback组件上的回调端口,Callback.js的错误端口连接到其他Output.js组件的in端口。运行图时,在运行时终端中收到错误:
“提供的回调必须是一个函数”
如何正确使用回调组件?
将Output的输出端口连接到Callback组件上的回调端口似乎并不正确,但这是边缘可以连接的唯一方法。