简体   繁体   English

使用javascript处理通用api中的缺失属性

[英]Handle missing properties in a general purpose api using javascript

I am working with a general purpose REST api that happens to exclude properties where the values are null (Spring Jackson @JsonInclude(Include.NON_NULL)) 我正在使用通用REST api,该api恰好排除了值为空的属性(Spring Jackson @JsonInclude(Include.NON_NULL))

Since I am just using Javascript (vue.js & axios) and generally not privy to what may or maynot be returned, what is the best way to handle missing properties in the axios 由于我只是使用Javascript(vue.js&axios)而通常不了解可能返回或可能不返回的内容,因此处理axios中缺少属性的最佳方法是什么?

response.data.expectedProprtyIsNotThere

in a general way so that is can be applied to all response objects without any additional code or duplication? 以一种通用的方式可以将其应用于所有响应对象,而无需任何其他代码或重复? I think I might be ok with have a default value of null or empty string. 我想我可以使用默认值为null或空字符串的方法。

I am not using any sdk and using the response data object directly. 我没有使用任何SDK,而是直接使用响应数据对象。

You could create a default response 您可以创建默认响应

var default = {...};

And before you use your response you insert the missing properties using Object.assign() : 在使用响应之前,请使用Object.assign()插入缺少的属性:

response = Object.assign({},default,response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM