【问题标题】:Redux-Form - Array of errorsRedux-Form - 错误数组
【发布时间】:2017-06-07 00:04:59
【问题描述】:

我一直在处理我的 react-native/redux 应用程序中包含错误的对象数组。

我有一个看起来像这样的 handleResponse 函数:

function handleResponse (response) {
  let status = JSON.stringify(response.data.Status)
  let res = JSON.stringify(response)
  if (Number(status) === 200) {
    return res
  } else {
    throw new SubmissionError('Something happened')
  }
}

但不是将纯文本作为参数传递给 SubmissionError 函数 - 我想以某种方式从对象数组中取出错误。

包含错误消息的对象数组如下所示:

{
  ErrorMessages: [
    { ErrorMessage: 'foo' },
    { ErrorMessage: 'bar' }
  ]
}

例如,我怎样才能抛出 foobar 错误?

【问题讨论】:

    标签: arrays react-native redux react-redux redux-form


    【解决方案1】:

    你不能真正同时throw 两件事,除非你将它们包装在 Promise 中或做其他技巧,否则我会将错误连接在一起并做一个 throw

    function handleResponse (res) {
      if (Number(res.data.Status) === 200) {
        return JSON.stringify(res)
      }
    
      const errors = res.ErrorMessages.map(e => e.ErrorMessage).join(', ')
      throw new SubmissionError(`Some errors occurred: ${errors}.`)
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-16
      • 2018-07-21
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      相关资源
      最近更新 更多