[BUG]: Deserialization doesnt work
MilanObrenovic opened this issue · 2 comments
MilanObrenovic commented
Version
5.1.0
Description
I have this model in TypeScript:
import {DateTime} from 'luxon';
import {JsonObject, JsonProperty} from 'typescript-json-serializer';
@JsonObject()
export class Coffee {
@JsonProperty()
public id: number;
@JsonProperty()
public coffeeName: string;
@JsonProperty()
public createdAt: DateTime;
@JsonProperty()
public updatedAt: DateTime;
}
This is how i fetch it
// ...
// This works and it fetched the whole array from API
let coffees = Coffee[];
const defaultSerializer = new JsonSerializer();
// Program breaks here
const coffeesDeserialized = defaultSerializer.deserialize(coffees, Coffee);
console.log(coffeesDeserialized);
Error
coffees.component.ts:37 ERROR TypeError: Cannot read properties of undefined (reading 'name')
at f.deserializeProperty (index.esm.js:1:7105)
at index.esm.js:1:2566
at Array.forEach (<anonymous>)
at f.deserializeObject (index.esm.js:1:2530)
at index.esm.js:1:3539
at Array.reduce (<anonymous>)
at f.deserializeObjectArray (index.esm.js:1:3509)
at f.deserialize (index.esm.js:1:1914)
at Object.next (coffees.component.ts:43:55)
at ConsumerObserver.next (Subscriber.js:91:33)
### Reproduction
Make a model in TS and fetch something from API and try to deserialize it
### On which OS the bug appears?
MacOS Ventura 13.0.1
### What is your project type?
Angular
### On which build mode the bug appears?
_No response_
### Anything else?
_No response_
MilanObrenovic commented
EDIT: solved it with "emitDecoratorMetadata": true
in the tsconfig.json
Thanks to #132 (comment)
This should be added in the docs as Frequently Encountered Problems and possible solutions
GillianPerard commented
It is explained in the first section of the readme (Installation).