'How to Assign an Object that got loaded from a Database to its class?
I am currently working on some logging functionality. This Logging is used inside my Repository where the logging is implemented inside a wrapper. At initalisation time the wrapper, and therefor the logging is applied to each function in the Repository. This is kinda working so far. My problem is the with the arguments that get passed to the logged function. These should always be an Object instance from one of my Modelklasses. When these are just got created using the new - operator everything is working fine. Problem is when they got loaded from the Database. I can not use modelArgu instanceof modelto identify if from which Class they got created. Is there any way to resign them to the original model?
I need this to know what kind of "uuid" I got to save it to the right param.
// always retrieves uuid from first given argument
function getUuid(model_type, ...args) {
console.log(args)
let uuid = null;
if (args[0] instanceof model_type) {
uuid = parseInt(args[0].uuid);
} else if (typeof args[0] === "number") {
uuid = args[i]
}
return uuid;
}
For example when the passed Object contained in args was just instantiated from some model class the console.log(args) would return Appointment {}which does exactly what I want. But when that argument gets loaded from some database. It instead returns something like:
{
uuid: 16,
description: 'asdfsf',
owner: 1,
from: '2022-02-15T09:48',
to: '2022-02-14T09:48',
participants: [],
files: [],
name: 'sdf'
}
This is still the Object but args[0] instanceof model_type returns false.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
