-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
- public navigation: European Parliament and Council = {};
- public navigation: Navigation = new Navigation;
- 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
@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.
– Renan Rodrigues
If you do not put the code in order for us to test and see the error the question is that it is wrong.
– Maniero
I am not an expert on typescript, but Voce could not use nested classes instead of simply declaring an object within the class?
– BrTkCa
I did not express myself badly I will pass the real state.
– Renan Rodrigues
It would be nice to [Edit] and add a playable code
– Jéf Bueno