Reputation: 13
i am trying to assign server response to an object, without success.
pls guide how to save this response to object?, have i defined user model correctly?
heres my user model
interface BelongsTo {
class_id: string;
section_id: string;
}
interface Userinfo {
_id: string;
name: string;
user_id: string;
roles: string[];
belongs_to: BelongsTo;
}
export interface IUser {
user: Userinfo
token: string
}
and the response from server is
{
message: "Success"
user:{
"_id": "60fbb4ec93eb4604658fc543",
"name": "Irfan",
"user_id": "irfan_123",
"roles": [
"student"
],
"belongs_to": {
"class_id": "60fbb3bb93eb4604658fc541",
"section_id": "60fbb3eb93eb4604658fc542"
},
"created_at": "2021-07-24T06:36:28.006Z",
"updated_at": "2021-07-24T06:36:28.006Z"
},
token: "eyJhbGciOiJIUzI1NiIs.5cCI6IkpXVCJ9.eyJCI6IkpXVCJCI6IkpXVCJ"
}
Upvotes: 1
Views: 104
Reputation: 2742
What is the name of the variable you have used as a server response? Is it something like myResponse
? If so, you can access the property user
inside that variable and finally store it in your desired variable this.user
.
this.user = myResponse.user; // myRespone is the variable you used to capture the server response and then you are accessing the user property.
Upvotes: 1
Reputation: 2171
use this:
let data = JSON.parse(response);
let userInfo = JSON.parse(data.user);
Upvotes: 0