Do this when you are completely stuck with NodeJS ORMs

Do this when you are completely stuck with NodeJS ORMs

Sometimes we do everything right, yet we don't have the expected results.

The Scenario

When working on a NodeJS application, do you often get an undefined value even if it exists in the respective object?

This happens to me all the time.

I am adding a small feature to an existing NodeJS application. We are using MongoDB amongst other databases. I've added the value in the respective row, the schema and appended the class interface as well since we are using TypeScript.

However, when referring the value itself, it's undefined. Therefore the function which uses this value becomes erratic, yet it's present when logging the object.

How to explain that?

Without thinking much, from experience, I just use a helper for dealing with Sequelize Objects, which is a method abstraction for stringifying and parsing objects. I know when using ORMs (Object Relational Mapper), an object is not a Plain Old JavaScript Object (POJO), that is, one consisting solely of data opposed to methods and prototypes. We have, depending on the libraries, instances of their classes, in this case MongoDB.

I just apply my helper method, called objectify, which is basically:

JSON.parse(JSON.stringify(value))

As a result, we have the value present as expected.