0

在 js-data 库中,我使用此处addAction()所示的方法添加了一个自定义查询。现在,当我的服务器在调用该自定义操作时返回 4xx 错误代码时,会引发错误,但找不到服务器响应(即发送):

store.getMapper('school').getTeacherReports(1234, {
  basePath: 'reports'
}).then(function(response) {
  console.log('response', response.data)
}).catch(function(err) {
  console.log('err', err);
})

我该如何处理?有什么我不知道我应该使用的方法吗?我已经根据文档response尝试了andresponseError属性。addAction()

4

2 回答 2

1

then函数可以接受两个参数:

then(
  onSuccess: Function,
  onRejection: Function
)

因此,您可以通过以下方式处理由于 4xx 错误而导致的拒绝:

store.getMapper('school').getTeacherReports(1234, {
  basePath: 'reports'
}).then(
  function (response) { // on success
    console.log('response', response.data);
  },
  function (error) { // on error
    console.error(error);
  },
)
于 2018-09-02T11:11:40.133 回答
0

这与 axios 有关:被拒绝的错误对象确实具有作为属性(源代码)的响应,但它不会在控制台输出中显示为这样,因为错误对象在控制台中的显示方式不同。下面显示了基础属性:

store.getMapper('school').getTeacherReports(1234, {
  basePath: 'reports'
}).then(function(response) {
  console.log('response', response.data)
}).catch(function(err) {
  let errorObject = JSON.parse(JSON.stringify(error))
  console.log(errorObject)
});
于 2018-09-03T08:12:43.480 回答