2017-06-07 30 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}.`) 
}