Estoy tratando de asignar la respuesta del servidor a un objeto, sin éxito.
Por favor, guíe cómo guardar esta respuesta en el objeto. ¿He definido correctamente el modelo de usuario?
aquí está mi modelo de usuario
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 }y la respuesta del servidor es
{ 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" }¿Cuál es el nombre de la variable que ha utilizado como respuesta del servidor? ¿Es algo como myResponse ? Si es así, puede acceder a la propiedad user dentro de esa variable y finalmente almacenarla en la variable deseada 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.utilizar este:
let data = JSON.parse(response); let userInfo = JSON.parse(data.user);