class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

TypeError: newValue_1.push is not a function

Open giovstef75 opened this issue 9 years ago • 2 comments

Hello, I get the following error whenever I try to plainToClass from json:

TypeError: newValue_1.push is not a function at http://localhost:4200/vendor.bundle.js:92475:36 at Array.forEach (native) at TransformOperationExecutor.transform (http://localhost:4200/vendor.bundle.js:92467:19) at _loop_1 (http://localhost:4200/vendor.bundle.js:92590:45) at TransformOperationExecutor.transform (http://localhost:4200/vendor.bundle.js:92613:17) at http://localhost:4200/vendor.bundle.js:92470:41 at Array.forEach (native) at TransformOperationExecutor.transform (http://localhost:4200/vendor.bundle.js:92467:19) at _loop_1 (http://localhost:4200/vendor.bundle.js:92590:45) at TransformOperationExecutor.transform (http://localhost:4200/vendor.bundle.js:92613:17) at ClassTransformer.plainToClass (http://localhost:4200/vendor.bundle.js:50796:25) at plainToClass (http://localhost:4200/vendor.bundle.js:7507:29) at MapSubscriber.project (http://localhost:4200/main.bundle.js:1547:128) at MapSubscriber._next (http://localhost:4200/vendor.bundle.js:15533:35) at MapSubscriber.Subscriber.next (http://localhost:4200/vendor.bundle.js:488:18)

This is the json:

{ "id": 1, "tipo": "O", "descrizione": "Menu Produzione", "posizione": 1, "nota": "", "menuRaggruppamenti": [ { "id": 1, "menuId": 1, "descrizione": "Anagrafiche", "descrizioneCorta": "Anagrafiche", "posizione": 1, "nota": "", "menuModuli": [ { "id": 1, "raggruppamentoId": 1, "descrizione": "Sviluppo Collezione Prodotto", "descrizioneCorta": "SPC", "posizione": 1, "nota": "", "moduloDefault": 0 }, { "id": 2, "raggruppamentoId": 1, "descrizione": "Configuratore Componenti", "descrizioneCorta": "Componenti", "posizione": 2, "nota": "", "moduloDefault": 0 } ] }, { "id": 2, "menuId": 1, "descrizione": "Tabelle", "descrizioneCorta": "Tabelle", "posizione": 2, "nota": "", "menuModuli": [ { "id": 4, "raggruppamentoId": 2, "descrizione": "Collezioni", "descrizioneCorta": "Collezioni", "posizione": 1, "nota": "", "moduloDefault": 0 }, { "id": 3, "raggruppamentoId": 2, "descrizione": "Contatti", "descrizioneCorta": "Contatti", "posizione": 2, "nota": "", "moduloDefault": 0 }, { "id": 17, "raggruppamentoId": 2, "descrizione": "Forme", "descrizioneCorta": "Forme", "posizione": 3, "nota": "", "moduloDefault": 0 }, { "id": 5, "raggruppamentoId": 2, "descrizione": "Fornitori", "descrizioneCorta": "Fornitori", "posizione": 4, "nota": "", "moduloDefault": 0 }, { "id": 6, "raggruppamentoId": 2, "descrizione": "Materiali", "descrizioneCorta": "Materiali", "posizione": 5, "nota": "", "moduloDefault": 0 } ] }, { "id": 3, "menuId": 1, "descrizione": "Reportistica", "descrizioneCorta": "Reportistica", "posizione": 3, "nota": "", "menuModuli": [ { "id": 8, "raggruppamentoId": 3, "descrizione": "Controllo Avanzamento Linea", "descrizioneCorta": "Avanzamento", "posizione": 1, "nota": "", "moduloDefault": 0 }, { "id": 9, "raggruppamentoId": 3, "descrizione": "Monitor Collezione", "descrizioneCorta": "Monitor", "posizione": 2, "nota": "", "moduloDefault": 1 } ] } ] }

It comes from a http call, this is the code:

    return this.http.authget(url)
        .map(response => <Menu>response.json())            
        .map(res => plainToClass(Menu, res as Object))
        .catch(this.handleError);

And these are the models:

import { MenuRaggruppamento } from "./menuraggruppamento"; import { Type, plainToClass } from "class-transformer";

export class Menu {

public id: number;
public tipo: string;
public descrizione: string;
public posizione: number;
public nota: string;
@Type(() => MenuRaggruppamento)
public menuRaggruppamenti: Array<MenuRaggruppamento>;

constructor() {
    this.id = 0;
    this.tipo = "";
    this.descrizione = "";
    this.posizione = 0;
    this.nota = "";
    this.menuRaggruppamenti = [];
}

}

import { MenuModulo } from "./menumodulo"; import { Type, plainToClass } from "class-transformer";

export class MenuRaggruppamento {

public id: number;
public menuId: number;
public descrizione: string;
public descrizioneCorta: string;
public posizione: number;
public nota: string;
@Type(() => MenuModulo)
public menuModuli: Array<MenuModulo>;

constructor() {
    this.id = 0;
    this.menuId = 0;
    this.descrizione = "";
    this.descrizioneCorta = "";
    this.posizione = 0;
    this.nota = "";
    this.menuModuli = [];
}

}

export class MenuModulo {

public id: number;
public raggruppamentoId: number;
public codice: string;
public descrizione: string;
public descrizioneCorta: string;
public posizione: number;
public nota: string;
public moduloDefault: boolean;

constructor(
) {
    this.id = 0;
    this.raggruppamentoId = 0;
    this.codice = "";
    this.descrizione = "";
    this.descrizioneCorta = "";
    this.posizione = 0;
    this.nota = "";
    this.moduloDefault = false;
}

}

My project has been created with Angular cli 1.0.0-beta.26, Angular 2.4.5

That's my package json:

{ "name": "gea-collection", "version": "1.0.0", "license": "MIT", "angular-cli": {}, "scripts": { "ng": "ng", "start": "ng serve --proxy-config proxy.conf.json --open true", "lint": "tslint "src//*.ts" --project src/tsconfig.json --type-check && tslint "e2e//*.ts" --project e2e/tsconfig.json --type-check", "test": "ng test", "pree2e": "webdriver-manager update --standalone false --gecko false", "e2e": "protractor" }, "private": true, "dependencies": { "@angular/common": "^2.4.5", "@angular/compiler": "^2.4.5", "@angular/core": "^2.4.5", "@angular/forms": "^2.4.5", "@angular/http": "^2.4.5", "@angular/platform-browser": "^2.4.5", "@angular/platform-browser-dynamic": "^2.4.5", "@angular/router": "^3.4.5", "@angular/upgrade": "^2.4.5", "bootstrap": "^3.3.7", "class-transformer": "^0.1.6", "core-js": "^2.4.1", "ng2-bootstrap": "^1.3.1", "primeng": "^1.1.4", "reflect-metadata": "^0.1.9", "rxjs": "^5.0.3", "typings": "^2.1.0", "zone.js": "^0.7.6" }, "devDependencies": { "@angular/compiler-cli": "^2.4.5", "@types/node": "^7.0.4", "angular-cli": "^1.0.0-beta.26", "codelyzer": "^2.0.0-beta.4", "font-awesome": "^4.7.0", "jasmine-core": "2.5.2", "jasmine-spec-reporter": "2.5.0", "karma": "1.2.0", "karma-chrome-launcher": "^2.0.0", "karma-cli": "^1.0.1", "karma-jasmine": "^1.0.2", "karma-remap-istanbul": "^0.2.1", "protractor": "^5.0.0", "ts-node": "^2.0.0", "tslint": "^4.4.2", "typescript": "^2.1.4" } }

Any idea? Thank you Giovanni

giovstef75 avatar Jan 31 '17 13:01 giovstef75

Duplicate of 44

giovstef75 avatar Jan 31 '17 14:01 giovstef75

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Aug 01 '20 00:08 github-actions[bot]