Existing Undefined return on javascript model

Asked

Viewed 290 times

-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;
}

In a certain page I’m starting in a variable before the builder and I do so:

public navigation: Navigation = new Navigation();

Down in the construtor I’m doing like this:

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

I can access the property hour and destiny but the line property for example does not work. On my console this gives the following error:

ERROR Error: Uncaught (in Promise): Typeerror: Cannot set Property 'isVia' of Undefined Typeerror: Cannot set Property 'isVia' of Undefined

I need to use model data.

What can be and how to fix ?

ATTEMPTS

  1. public navigation: European Parliament and Council = {};
  2. public navigation: Navigation = new Navigation;
  3. public navigation: Navigation; and the constructor this.navigation = Navigation();

Both occur the same error.

SENARY

I’m developing in ionic 2 basically he works with angular 4 and uses typescript as a language. When I create an Ionic 4 page it generates files in (.html, .ts, .scss, .modeule.ts) the file .ts controls my pages, in it I can easily manipulate all my html, making requests to the server and changing the screen at runtime easily. For better development I use the idea of models to standardize my content both and receiving and 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 all file) in my .ts of my page I am instantiating this my model, speaking that my variable navigation will take the shape of my model Navigation soon after my constructor having added a value, for how I informed ascima use in my html. Right now I’m trying the mistake I’m expressing in this question.

To reproduce the error it is necessary to use a model "complex" or that has object of objects, can be observed in mine that I have values at the root (destiny, hour) and an object of line inside this object I have others, I cannot access this object of line and nothing inside of it.

  • I couldn’t reproduce the error. I circled on the Playground and it was beautiful. https://www.typescriptlang.org/play

  • @Maniero You are reproducing wrong, the error happens when it is an object of objects, as in my example. I can fully access Destiny and hour that "is at the root of the object" however line that within it has _Organization that is an object, via that is also object and even isVia that this within line is not found.

  • If you do not put the code in order for us to test and see the error the question is that it is wrong.

  • I am not an expert on typescript, but Voce could not use nested classes instead of simply declaring an object within the class?

  • I did not express myself badly I will pass the real state.

  • It would be nice to [Edit] and add a playable code

Show 1 more comment

1 answer

0

The solution to my problem was to mess 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 was my line object, as nothing initialized it was empty, so I had this change or so before I set a value initialized it as {} in this way this.navigation.line = {}

Either of the two solutions would solve my problem.

Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.