Mix Austria
Mix Austria

Reputation: 935

Argument of type 'User' is not assignable to parameter of type 'string'

I have created an interface of User to put in some value from ngModel. Take a look at this.

user.ts

export class User{
    _id: String;
    username: String;
    password: String;
    firstname: String;
    lastname: String;
    gender: String;
    address: String;
    contact: Number;
    email?: String;
}

Example of ngModel binding

<input type="text" [(ngModel)]="user.username" class="form-control" name="username" id="username" placeholder="Enter your Username" formControlName="username"/>

Output from the console log

enter image description here

I want to make the result to a json. I use JSON.parse but the error on the title pops up. How can I convert it to a json object? Thanks

Upvotes: 3

Views: 2729

Answers (1)

Jason W
Jason W

Reputation: 13179

JSON.parse is meant for creating a javascript object from a string (that is already formatted for JSON). What you're looking for is JSON.stringify instead to convert your javascript object into JSON text.

Upvotes: 5

Related Questions