Undefined property return in javascript model

-1

I have a well-defined model, written this way:

export class Navigation {
  line: {
    _id: string;
    code: string;
    name: string;
    isVia: boolean;
    _operator: {
      _id: string;
      name: string;
    };
    via: {
      _id: string;
      name: string;
    }
  };
  hour: string;
  destiny: string;
}

On a certain page I'm starting in a variable before the constructor and I do this:

public navigation: Navigation = new Navigation();

Down in construtor I'm doing this:

this.navigation.destiny = 'center';
this.navigation.line.isVia = false;
this.navigation.line.via.name = 'Nenhuma via para esta linha';

I can access the properties hour and destiny however the properties for example of line does not work. In my console is giving the following error:

  

ERROR Error: Uncaught (in promise): TypeError: Can not set property 'isVia' of undefined   TypeError: Can not set property 'isVia' of undefined

I need to use the model data.

What can be and how to arrange?

TRY

  • public navigation: Navigation = {};
  • public navigation: Navigation = new Navigation;
  • public navigation: Navigation; and in the this.navigation = Navigation ();
  • Both of these occur the same error.

    SENIOR

    I'm developing on ionic 2 basically it works with angular 4 and uses typescript as language. When I create a page in 'ionic 4' it generates files in ( .html , .ts , .scss , .modeule.ts ) the .ts file controls my pages, I can easily handle all my html making requests to the server and changing the screen at runtime easily. For a better development I use the idea of models to standardize my content both receive and receive when sending, based on this everything I receive / send has the formulation of a model . The model is a separate file, in case mine is expressed in its total form (ie every file) in my .ts of my page I am instantiating this my model, saying that my variable navigation will take the form of my model Navigation shortly after my constructor having added a value, for as I told ascima use in my html. At this point I am trying the error that I express in this question.

    To reproduce the error it is necessary to use a model "complex", that is to say that it has objects object, it can be observed in mine that I have values in the root ( destiny , hour ) and a line object % within this object I have others, I can not access this object from line and nothing inside it.

        
    asked by anonymous 20.10.2017 / 14:49

    1 answer

    0

    The solution to my problem was to tinker with the class.

    export class Navigation {
      line: any = {
        _id: String,
        code: String,
        name: String,
        isVia: Boolean,
        _operator: {
          _id: String,
          name: String,
        },
        via: {
          _id: String,
          name: String,
        }
      };
      hour: String;
      destiny: String;
    }
    

    This happens because my model was not able to find reference to what my line object was, as nothing was initializing it was empty, so I had this change or else I before setting a value initialized it as {} in this way this.navigation.line = {}

    Either solution would solve my problem.

    Thank you.

        
    20.10.2017 / 15:48