如何使用 rxjs Observables 处理可能是空对象的 API 响应?
How to handle an API response that might be an empty object with rxjs Observables?
前端后端都可以编辑
我有一项获取公报的服务。此公报可能存在(有一个公报处于活动状态)或可能不存在(没有公报处于活动状态),因此 API 可能会以公报的 JSON 对象或空的 JSON对象。
我是这样处理的:
getCommunique(): Observable<Communique | {}> {
return this.http.get(this.apiUrl + '/getCommunique').map((response) => {
if (Object.keys(response.json()).length != 0) {
return new Communique(response.json())
} else {
return {}
}
});
}
这是我的 Communique
class:
export class Comunique {
public id: number;
public title: string;
public content: string;
public date: Date;
public url: string;
public imageUrl: string;
constructor(input: ComuniqueRaw) {
this.id = input.id;
this.title = input.title;
this.content = input.content;
this.date = new Date(input.date.timestamp * 1000);
this.url = 'url' + input.slug;
this.imageUrl = 'url' + input.image;
}
}
这行得通...但这似乎是一种解决方法。 observable 不应该只有 return Communique
对象吗?但是,如果没有活动公报,将如何创建 Communique
对象?我的意思是,我可以在 Communique
class 中添加一个类似 public exists: boolean;
的字段,但这似乎也是一种解决方法(一个只包含一个 boolean
的对象) .
我的处理方式正确吗?或者...如何使用 rxjs Observables 处理可能是空对象的 API 响应?
可观察到的 http.get() 总是 return 一次且仅一次。如果服务器以空对象响应,则忽略它不是 http 客户端的责任 - 这是一个有效的 http 响应。
我会说如果没有公报对象,你的服务器应该 return 一个 404,你用 rxjs catchError 捕获它,它会单独处理这种情况。
编辑:
如果您希望总是 return 带有空响应或对象的 200,那么您实施的模式似乎不错。
编辑2:
我刚刚注意到您使用的是旧的 .map() 方法。我的 catchError() 答案是使用相对较新的管道运算符。
getCommunique(): Observable<Communique> {
return this.http.get(this.apiUrl + '/getCommunique').pipe(
map(response => new Communique(response.json())),
catchError(() => of(null))
);
}
前端后端都可以编辑
我有一项获取公报的服务。此公报可能存在(有一个公报处于活动状态)或可能不存在(没有公报处于活动状态),因此 API 可能会以公报的 JSON 对象或空的 JSON对象。
我是这样处理的:
getCommunique(): Observable<Communique | {}> {
return this.http.get(this.apiUrl + '/getCommunique').map((response) => {
if (Object.keys(response.json()).length != 0) {
return new Communique(response.json())
} else {
return {}
}
});
}
这是我的 Communique
class:
export class Comunique {
public id: number;
public title: string;
public content: string;
public date: Date;
public url: string;
public imageUrl: string;
constructor(input: ComuniqueRaw) {
this.id = input.id;
this.title = input.title;
this.content = input.content;
this.date = new Date(input.date.timestamp * 1000);
this.url = 'url' + input.slug;
this.imageUrl = 'url' + input.image;
}
}
这行得通...但这似乎是一种解决方法。 observable 不应该只有 return Communique
对象吗?但是,如果没有活动公报,将如何创建 Communique
对象?我的意思是,我可以在 Communique
class 中添加一个类似 public exists: boolean;
的字段,但这似乎也是一种解决方法(一个只包含一个 boolean
的对象) .
我的处理方式正确吗?或者...如何使用 rxjs Observables 处理可能是空对象的 API 响应?
可观察到的 http.get() 总是 return 一次且仅一次。如果服务器以空对象响应,则忽略它不是 http 客户端的责任 - 这是一个有效的 http 响应。
我会说如果没有公报对象,你的服务器应该 return 一个 404,你用 rxjs catchError 捕获它,它会单独处理这种情况。
编辑:
如果您希望总是 return 带有空响应或对象的 200,那么您实施的模式似乎不错。
编辑2:
我刚刚注意到您使用的是旧的 .map() 方法。我的 catchError() 答案是使用相对较新的管道运算符。
getCommunique(): Observable<Communique> {
return this.http.get(this.apiUrl + '/getCommunique').pipe(
map(response => new Communique(response.json())),
catchError(() => of(null))
);
}