Angular 缺少元素时如何防止映射失败
有没有一种方法可以使映射更能防止失败?当数据通过以下代码传入时,我实现了一个映射Angular 缺少元素时如何防止映射失败,angular,Angular,有没有一种方法可以使映射更能防止失败?当数据通过以下代码传入时,我实现了一个映射 getSurveyRequest(surveyId: string) { return this.api.get({endpoint: '/survey/request/' + surveyId}) .pipe( map(res => res as IApiResponseBody<ISurvey[]>),
getSurveyRequest(surveyId: string) {
return this.api.get({endpoint: '/survey/request/' + surveyId})
.pipe(
map(res => res as IApiResponseBody<ISurvey[]>),
map((res: IApiResponseBody) => { res.Data = SurveyRequestService.adaptMainSurveyList(res.Data);
console.log(res.Data);
if ( res.Success === true) {
return res.Data;
}
return null;
})
);
}
所有这些都可以正常工作,但当数据元素中没有contact或property元素时,它会完全失败,当存在空contact和property元素时,它会再次工作。因此,我不确定为什么会发生这种情况,以及我能做些什么来提高代码的健壮性,以防api返回一些格式错误的数据,这不会导致大问题。检查响应的所有属性
static adaptMainSurveyList(surveylist): ISurvey {
if (surveylist) {
return {
docId: surveylist.docId ,
... (surveylist.contact && firstName: surveylist.contact.fname) ,
... (surveylist.contact && lastName: surveylist.contact.lname) ,
... (surveylist.contact && email: surveylist.contact.email),
... (surveylist.contact && phone: surveylist.contact.phone),
... (surveylist.property && address1: surveylist.property.address1) ,
// likewise for the rest
};
}
}
这将阻止读取未定义的上的属性。现在,数据只填充服务器响应中存在的值。这意味着,当您使用adaptemainsurveylist
的输出时,您需要在使用某个属性之前检查它是否存在
如果您希望将默认空值设置为响应数据中不存在的值,那么下面是一种方法
static adaptMainSurveyList(surveylist): ISurvey {
if (surveylist) {
return {
docId: surveylist.docId || null,
firstName: (surveylist.contact && surveylist.contact.fname) || null ,
lastName: (surveylist.contact && surveylist.contact.lname) || null,
email: (surveylist.contact && surveylist.contact.email) || null,
phone: (surveylist.contact && surveylist.contact.phone) || null,
address1: (surveylist.property && surveylist.property.address1) || null ,
// likewise for the rest
};
}
}
您正在寻找的是一种安全树遍历的方法,不幸的是,Typescript不支持一种开箱即用的方法来执行简单、干净的安全树遍历 当然,您可以对相当于几个内联三元语句的语句进行逻辑检查。请参见下面的示例
静态AdapterMainSurveyList(surveylist):IsSurvey{
if(调查列表){
返回{
docId:surveylist.docId,
名字:surveylist.contact?surveylist.contact.fname:null,
lastName:surveylist.contact?surveylist.contact.lname:null,
电子邮件:surveylist.contact?surveylist.contact.email:null,
电话:surveylist.contact?surveylist.contact.phone:null,
address1:surveylist.property?surveylist.property.address1:null,
address2:surveylist.property?surveylist.property.address2:null,
城市:surveylist.property?surveylist.property.city:null,
状态:surveylist.property?surveylist.property.state:null,
zip:surveylist.property?surveylist.property.zip:null,
状态:surveylist.property?surveylist.property.status:null
};
}
}
这可能很好,并且可能是使用当前数据结构所能获得的最好结果。不幸的是,如果你需要降低一个或两个以上的级别,你的三元操作符最终会有几个条件,这看起来很糟糕
但是,如果您不介意使用第三方库,lodash有一个函数,称为
获取对象路径处的值。如果解析的值未定义,则返回defaultValue
自
3.7.0
论据
- 对象(object):要查询的对象
- path(Array | string):要获取的属性的路径
- [defaultValue](*):为未定义的解析值返回的值
静态AdapterMainSurveyList(surveylist):IsSurvey{
if(调查列表){
返回{
docId:u2;.get(surveylist,“docId”,null),
firstName:u.get(surveylist,“contact.fname”,null),
lastName:u.get(surveylist,“contact.lname”,null),
电子邮件:u.get(surveylist,“contact.email”,null),
电话:u.get(surveylist,“contact.phone”,null),
address1:u2;.get(surveylist,“property.address1”,null),
address2:u2;.get(surveylist,“property.address2”,null),
城市:u.get(surveylist,“property.city”,null),
state:u.get(surveylist,“property.state”,null),
zip:uzy.get(surveylist,“property.zip”,null),
状态:u.get(surveylist,“status”,null)
};
}
}
作为旁注,您实际上可以在模板中使用带插值的Angular进行此操作。角度模板中的{code>{{a?.b?.c?.d}是模板安全树遍历的有效方法,但它在Typescript中不起作用。对此有任何要求吗?在我的例子中,当我使用这些代码时,我会遇到很多错误。它抱怨无法找到firstName、lastName等,并且在严格模式下,对象文字不能有多个同名属性。在使用方法
AdapterMainSurveyList
的输出时,需要检查属性。对于“在严格模式下,对象文字不能有多个同名属性”的错误,是否确实复制了正确的答案代码?因为同一属性不会被使用两次。如果您希望设置默认的空值,请参阅我的编辑答案看起来像一个干净的解决方案。有一件事,您的代码是关闭的,因为您不需要在path中指定surveylist。这是对象的名称,因此要访问docId,您的路径仅为docId而不是surveylist.docId
static adaptMainSurveyList(surveylist): ISurvey {
if (surveylist) {
return {
docId: surveylist.docId || null,
firstName: (surveylist.contact && surveylist.contact.fname) || null ,
lastName: (surveylist.contact && surveylist.contact.lname) || null,
email: (surveylist.contact && surveylist.contact.email) || null,
phone: (surveylist.contact && surveylist.contact.phone) || null,
address1: (surveylist.property && surveylist.property.address1) || null ,
// likewise for the rest
};
}
}