javascript - 使用解析的 json 作为 javascript 对象时出现“未定义”参数错误
问题描述
我的Angular
申请收到以下信息json
{isSignedIn: true, additional-info: ""{\"external-profile\":{\"email\":\"manu.chadha@ho…\"firstname\":\"Manu\",\"lastname\":\"Chadha\"}}""}
我将 解析additional-info
为类型的对象UserProfileAPI
。
let resultObject =JSON.parse(subscribed['additional-info']) as UserProfileAPI;
export class UserProfileAPI {
'external-profile': User;
constructor(externalProfile:User){
this['external-profile'] = externalProfile;
}
}
export class User {
constructor (public firstname:string,
public lastname:string,
public email:string,
public password:string=""){}
}
我的component
班级有一个变量profile:UserProfileAPI;
,我想将收到的值映射json
到profile
.
我已经profile
初始化ngOnInit
this.profile = new UserProfileAPI(new User("","","",""))
但是当我尝试映射json
let resultObject =JSON.parse(subscribed['additional-info']) as UserProfileAPI;
console.log("parsed additional info is ",resultObject);
this.profile["external-profile"].email = resultObject["external-profile"].email;
this.profile["external-profile"].firstname = resultObject["external-profile"].firstname;
this.profile["external-profile"].lastname = resultObject["external-profile"].lastname;
我收到错误ERROR TypeError: Cannot read property 'email' of undefined
at SafeSubscriber._next (nav-component.component.ts:91)
at
我做错了什么?
解决方案
推荐阅读
- razor - 如何修复 Visual Studio 2019 中 chtml 页面显示的错误
- html - 仅使用css将文本悬停时如何使标签弹出
- javascript - Reactjs,将数据(id)从一个组件传递到另一个组件以下载歌曲
- javascript - 在 Javascript 中实现单次迭代
- laravel - Laravel Api.php "message": "语法错误,意外标识符 \"Route\"", "exception": "ParseError",
- ghostscript - CMap 定义中的 Ghostscript /stackunderflow
- html - 通过在当前单元格上应用 CSS(HTML 和 CSS),将 CSS 应用到前一个单元格/第 n 个单元格
- .net - ActionFilter By Property 属性中的更改请求
- javascript - 我需要这些 JavaScript 数组中的每个第一句正确显示,并且正确地包含在其中的星期几
- java - CLI 的 quarkus 开发模式,如何重新启动应用程序