Avro-js problem

After a few years I'm having a play with avro serialisation using the npm avro-js package within a function node. I've tried the avro node that comes with the Kafka nodes in the NR library but I need to be able to feed both the data and the schema to a node as part of the msg object.

I've installed avro-js using npm and inserted avro:require('avro-js') into the functionGlobalContext section of settings.js.

I have then tried the simple encoding example on the avro-js website as follows. I've added the first line.

var avro= global.get('avro');
var type = avro.parse({
name: 'Pet',
type: 'record',
fields: [
{name: 'kind', type: {name: 'Kind', type: 'enum', symbols: ['CAT', 'DOG']}},
{name: 'name', type: 'string'}
]
});
var pet = {kind: 'CAT', name: 'Albert'};
var buf = type.toBuffer(pet); // Serialized object.
var obj = type.fromBuffer(buf); // {kind: 'CAT', name: 'Albert'}

It never gets to the serialisation part (toBuffer)and crashes out at avro.parse with "Error: non-array pet fields", whereas fields is obviously an array of objects.

Any ideas?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.