Answer the question
In order to leave comments, you need to log in
Why doesn't JSON.parse eat the string?
I request an object from the backend like this:
service:
@Injectable()
export class UserService {
constructor(private httpClient: HttpClient) { }
authUser(login) {
return this.httpClient.get(Config.host + 'users/get_user?login=' + login);
}
}
submit() {
const login = this.form.controls.login.value;
this.userService.authUser(login).subscribe(user => {
console.log(typeof user, user)
console.log(JSON.parse(user));
});
}
string [{"model": "app_users.user", "pk": 1, "fields": {"login": "[email protected]", "fname": "kalinin", "lname": "sergey"}}]
webpack: Compiled successfully.
ERROR in src/app/components/login/login.component.ts(27,30): error TS2345: Argument of type 'Object' is not assignable to parameter of type 'string'.
Answer the question
In order to leave comments, you need to log in
This error is not a runtime error, but a compilation error. Watch the types. Try this console.log(JSON.parse(user)); replaced byconsole.log(JSON.parse(user.toString()));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question