# Avro-js problem

**URL:** <https://discourse.nodered.org/t/avro-js-problem/46589>\
**Category:** General\
**Created:** [2 June 2021 11:29 UTC](https://discourse.nodered.org/t/avro-js-problem/46589 "2021-06-02T11:29:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisofCCL](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisofccl/32/42442_2.png) [@chrisofCCL](https://discourse.nodered.org/u/chrisofCCL)\
**Post date:** [2 June 2021 11:29 UTC](https://discourse.nodered.org/t/avro-js-problem/46589/1 "2021-06-02T11:29:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 August 2021 11:30 UTC](https://discourse.nodered.org/t/avro-js-problem/46589/2 "2021-08-01T11:30:17Z")

</div>

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