javascript - Angular - undefined to null field conversion -


i developing angular application spring backend. have formgroup follows:

myform = new formgroup({   fieldone: new formcontrol(),   fieldtwo: new formcontrol(),   fieldthree: new formcontrol() }); 

that matches on object this:

export class myclass{     firstfield: number;     secondfield: number;     thirdfield: string; } 

when submit form this:

this.firstfield = myform.value.fieldone; 

the problem if fields not set, undefined. causing problem backend service cannot deserialize object because doesn't expects undefined null.

this solve problem:

this.firstfield = myform.value.fieldone ? myform.value.fieldone : null; 

but when have lot of fields it's not elegant. found following works:

export class myclass{     firstfield: number = null;     secondfield: number = null;     thirdfield: string = null; } 

it perfect. question is, since i'm new javascript: correct? there best practice kind of conversion? how solve kind of serialization problems between frontend , backend? thanks.

as @manu has pointed out can assign null values. , can define default values well. follows

export class myclass{     firstfield: number = 0;     secondfield: number = 0;     thirdfield: string = ''; } 

so here don't need handle undefined or null value. , practice initialize values this.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -