当使用 NgRx getByKey 通过给定的主键从 Strapi 获取数据库记录时,该主键不存在我收到以下错误:
Cannot destructure property 'error' of 'sourceError'
来自后端的响应是 404 Not Found。
例子:
this.projectEntityService.getByKey(this.id).subscribe(
(project) => {
... populate form fields
},
(error) => {
... show error, navigate away, etc.
})
如果找到数据,那么我们将填充表单等,但如果发生类似 404 的错误,没有找到数据,那么什么也不会发生,我无法注销错误、导航等。
我相信这是因为 NgRx 数据引发了错误,因为消息说它无法解构错误的属性:
core.js:6210 ERROR TypeError: Cannot destructure property 'error' of 'sourceError' as it is undefined.
at extractMessage (ngrx-data.js:556)
at new DataServiceError (ngrx-data.js:551)
at ngrx-data.js:1187
at CatchSubscriber.selector (ngrx-data.js:2125)
at CatchSubscriber.error (catchError.js:27)
at MapSubscriber._error (Subscriber.js:75)
at MapSubscriber.error (Subscriber.js:55)
at MapSubscriber._error (Subscriber.js:75)
at MapSubscriber.error (Subscriber.js:55)
at FilterSubscriber._error (Subscriber.js:75)
// Many ways the error can be shaped. These are the ways we recognize.
function extractMessage(sourceError) {
const { error, body, message } = sourceError;
现在这个错误,似乎它可能是由于后端的错误响应只是状态 404 Not Found 而发生的。
谢谢!