class-transformer: TypeError: Cannot read property 'prototype' of undefined

I am having three classes Person, BasicDetails and CloudResponse, when I extend BasicDetails class in Person class and then I run the following code it gives me error.

return this.http.post(Environment.domain, JSON.stringify(requestObj), {headers: this.headers})
      .map((res: Response) => {
        return res.json();
      })
      .map(res => plainToClass(MeraCRMCloudResponse, res as Object))
      .catch((error: any) => Observable.throw(console.log(error)));

TypeError: Cannot read property ‘prototype’ of undefined at http://localhost:4200/main.bundle.js:60194:84 at Array.filter (native) at MetadataStorage.findMetadatas (http://localhost:4200/main.bundle.js:60194:46) at MetadataStorage.findTransformMetadatas (http://localhost:4200/main.bundle.js:60098:21) at TransformOperationExecutor.applyCustomTransformations (http://localhost:4200/main.bundle.js:59887:58) at _loop_1 (http://localhost:4200/main.bundle.js:59854:41) at TransformOperationExecutor.transform (http://localhost:4200/main.bundle.js:59876:31) at http://localhost:4200/main.bundle.js:59736:41 at Array.forEach (native) at TransformOperationExecutor.transform (http://localhost:4200/main.bundle.js:59733:19)

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 3
  • Comments: 25 (6 by maintainers)

Commits related to this issue

Most upvoted comments

I made a naive fix long time ago in my fork. It worked in my situation, but I have no idea if it might have any implications somewhere else. https://github.com/britvik/class-transformer/commit/0a1144ce787ec04b19088a621f8e905b35852875

I thought I had a solution simply by using @Type decorators everywhere, but it didn’t always work. I still got this error when trying to use plainToClass, where the plain object contains extra fields that are complex objects.

For now, I use the solution from @britvik (thanks!). But I prefere to keep using the original library instead of cloning it… So here’s my ugly patch, which I apply when my application starts :

import { defaultMetadataStorage } from 'class-transformer/storage';
// ...
defaultMetadataStorage['findMetadatas'] = (metadatas: any, target: any, propertyName: any) => {
  const metadataFromTarget = metadatas.filter(
    (meta: any) => (target === undefined || meta.target === target) && meta.propertyName === propertyName
  );
  const metadataFromChildren = metadatas.filter(
    (meta: any) => target && target.prototype instanceof meta.target && meta.propertyName === propertyName
  );
  return metadataFromChildren.reverse().concat(metadataFromTarget.reverse());
};

I don’t know if it is going to work properly in the long run, but it seems to work for now. I wish @pleerock Iooks into this issue!

Just published 0.1.9, please test it and report if you still encounter this issue. Thanks!