2017-06-07 26 views
0

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

我有一个用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' } 
    ] 
} 

如何我举个例子,扔酒吧错误?

回答

1

你不能一次真正throw两两件事,除非你将它们包装在一个承诺或做其他的招数,我只想拼接误差一起做单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}.`) 
}