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

StructuredClone() function converts entity object into plain object, causing undefined properties when calling getters #117

Closed
fetahokey opened this issue May 14, 2023 · 2 comments

Comments

@fetahokey
Copy link

Summary

using the structuredClone() function, as it creates a deep copy of the object, which means it will lose any functions, getters, or setters that were defined on the original object. Instead, you will end up with a plain object that only contains the data properties.

Description:

When using thegetPropsCopy()method to create a copy of an entity object, the StructuredClone() function is converting the entity object into a plain object. This is causing issues when calling the getters for properties of the entity object.

For example, when trying to access the address.country, address.postalCode, and address.street getters of a UserEntity object, the properties are returning undefined due to the conversion of the object by StructuredClone().

This issue seems to be related to the StructuredClone() function, and may be causing similar issues in other parts of the application.

 public getPropsCopy(): EntityProps & BaseEntityProps {
    const propsCopy = structuredClone({
      id: this._id,
      createdAt: this._createdAt,
      updatedAt: this._updatedAt,
      ...this.props,
    });
    return Object.freeze(propsCopy);
  }

Steps to Reproduce:

ref to. >

 toPersistence(entity: UserEntity): UserModel {
    const copy = entity.getPropsCopy();
    const record: UserModel = {
      id: copy.id,
      createdAt: copy.createdAt,
      updatedAt: copy.updatedAt,
      email: copy.email,
      country: copy.address.country, // getter
      postalCode: copy.address.postalCode, // getter
      street: copy.address.street, // getter
      role: copy.role,
    };
    return userSchema.parse(record);
  }
  • Create an instance of a UserEntity object.
  • Call the getPropsCopy() method to create a copy of the object.
  • Try to access the address.country, address.postalCode, and address.street getters of the copied object.

Expected Result:

The address.country, address.postalCode, and address.street getters should return the expected values.

Actual Result:

The address.country, address.postalCode, and address.street getters are returning undefined.

@Sairyss
Copy link
Owner

Sairyss commented May 14, 2023

Thanks, just fixed it

@Sairyss Sairyss closed this as completed May 14, 2023
@fetahokey
Copy link
Author

Thanks, just fixed it

I'm impressed with the speed of your response! Thank you so much @Sairyss .
I will make sure to pull the latest changes and keep you informed of any updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants