javascriptclassobjectfastifybull-queue

Object no longer instance of Class after adding to Bull queue


In the handler of my Fastify route, I'm adding to my Bull queue an object of a class. In the queue's process method, this object is no longer an instance of the class. Is this normal or am I missing some (or a lot) of the basics?

const fastify = require('fastify')({ logger: true })
const Queue = require('bull');
let addQueue = new Queue('Add', 'redis://127.0.0.1:6379');

...

addQueue.process(function(job, done){
  console.log(job.data.dataObj instanceof ClasssDefinedSomwhereElse);
}

...

fastify.route({
  method: 'POST',
  url: '/',
  ...
  handler: async (request, reply) => {
    if(isFirstRequest) {
      classObj = new ClasssDefinedSomwhereElse();
      isFirstRequest = false;
    } else {
      console.log(classObj instanceof ClasssDefinedSomwhereElse);
      addQueue.add({dataObj: classObj});
    }

addQueue.add({dataObj: classObj}); or addQueue.add(classObj); makes no difference. The handler prints true & the process method prints false. Why is this?

What I'm eventually trying to do is, in the process method, call a method of the class using the object that I've just passed in. I'm not from a Javascript background so I think I might've missed something basic. Any help is appreciated.


Solution

  • In javascript, the class, is not the same as class in Object-Oriented Programming. The class is just syntactic sugar for the prototype

    So what is happening in your software is that:

    In this process, there is no information about the ClasssDefinedSomwhereElse (like it would be in a class serialization in Java). So you need to create a new instance of that class, based on the simple JSON that Bull is fetching.