Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Cannot read properties of undefined (reading 'name') #221

Closed
anwarramadha opened this issue May 28, 2024 · 1 comment
Closed

[BUG]: Cannot read properties of undefined (reading 'name') #221

anwarramadha opened this issue May 28, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@anwarramadha
Copy link

anwarramadha commented May 28, 2024

Version

6.0.0

Description

Version

6.0.1

Description

This error shows when I try to deserialize json data which has type Object or Array. I have added emitDecoratorMetadata and experimentalDecorators in the tsconfig.json as mentioned in the documentation.

Error

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'name')
    at f2.deserializeProperty (typescript-json-serializer.js?v=5ac02d34:210:270)
    at typescript-json-serializer.js?v=5ac02d34:115:35
    at Array.forEach (<anonymous>)
    at f2.deserializeObject (typescript-json-serializer.js?v=5ac02d34:114:16)
    at f2.deserialize (typescript-json-serializer.js?v=5ac02d34:88:87)
    at useWhatsapp.ts:45:18
    at callWithErrorHandling (chunk-G3HSUYBM.js?v=5ac02d34:1750:18)
    at callWithAsyncErrorHandling (chunk-G3HSUYBM.js?v=5ac02d34:1758:17)
    at job (chunk-G3HSUYBM.js?v=5ac02d34:3199:9)
    at callWithErrorHandling (chunk-G3HSUYBM.js?v=5ac02d34:1750:32)

Reproduction

I have a json data:

{
    "meta": {
        "total_items": 8,
        "per_page": 10,
        "page": 1
    },
    "data": [
        {
            "id": "1083459326053051",
            "name": "info_produk",
            "category": "MARKETING",
            "language": "id",
            "status": "APPROVED",
            "created_at": "2023-09-27T06:13:48.285Z",
            "updated_at": "2023-10-11T09:13:52.095Z",
            "created_by": "timdev 123",
            "updated_by": "thalissacivitas"
        },
        {
            "id": "416180507847625",
            "name": "info_masuk",
            "category": "UTILITY",
            "language": "id",
            "status": "APPROVED",
            "created_at": "2023-09-27T06:13:48.285Z",
            "updated_at": "2023-10-11T09:13:52.095Z",
            "created_by": "timdev 123",
            "updated_by": "thalissacivitas"
        }
    ]
}

and a WhatsappTemplate class:

@JsonObject()
export class WhatsappTemplate {
  @JsonProperty ('id') id: string = '';
  @JsonProperty ('name') name: string = '';
  @JsonProperty ('content') content: string = '';
  @JsonProperty ('status') status: string = '';
  @JsonProperty ('created_by') createdBy: string = '';
  @JsonProperty ('created_at') createdAt: Date = new Date();
  @JsonProperty ('updated_by') updatedBy: string = '';
  @JsonProperty ('updated_at') updatedAt: Date = new Date();
  @JsonProperty ('languange') languange: string = '';
  @JsonProperty ('category') category: string = '';

  constructor(init: Partial<WhatsappTemplate>) {
    Object.assign(this, init);
  }
}

This is my tsconfig.json

{
    "compilerOptions": {
      "target": "es5",
      "lib": ["es5", "dom"],
      "types": ["cypress", "node"],
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
    },
    "include": ["**/*.ts"]
  }

when I try to deserialize this data into WhatsappTemplate class Array, this return the error I mentioned before.

const serializer = new JsonSerializer({
  formatPropertyName: (name: string) => name
})
serializer.deserialize(value.data, WhatsappTemplate)

This issue doesn't appear when I remove JsonProperty as in this comment #132 (comment). But this makes the returned data empty.

Does the typescript-json-serializer support nuxt js 3?

On which OS the bug appears?

Windows 10

What is your project type?

NuxtJS 3.4.3

On which build mode the bug appears?

Development

Anything else?

No response

@anwarramadha anwarramadha added the bug Something isn't working label May 28, 2024
@GillianPerard
Copy link
Owner

Hi, sorry to be late.

I never use Nuxt but it seems there is an issue about the reflect-metadata lib and esbuild: nuxt/nuxt#15977 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants