从fetch()获得的响应是
{errors: {"": [,…]}, type: "https://tools.ietf.org/html/rfc7231#section-6.5.1",…}
errors: {"": [,…]}
"": [,…]
0: "Error converting value \"field.value\" to type 'System.Collections.Generic.List`1[System.String]'. Path '', line 1, position 13."
status: 400
title: "One or more validation errors occurred."
traceId: "00-fc1fd022e2c5545a24909a8c7a15c38d-f930263d77c54f34-00"
type: "https://tools.ietf.org/html/rfc7231#section-6.5.1"
我能够从对象中重构errors, status, title, traceId, and type
,但由于重构errors
是另一个对象,其中包含一个匿名数组,因此我不知道如何进一步对其进行重构以访问该数组。
发布于 2022-09-06 21:11:35
您可以对其进行重构,但需要为其提供有效的标识符。
const res = { errors: { "": ['an error'] } }
const { errors: { "": messages }} = res
console.log(messages) // ['an error']
尽管如此,我不认为这是非常易读的代码。这里发生了什么很让人困惑。我认为这样做可能要清楚得多:
const messages = res.errors['']
最后,在名为空字符串的属性上提供数据是非常奇怪的。这似乎也是个坏主意,因为它会产生像整个问题一样的混乱。
https://stackoverflow.com/questions/73627684
复制相似问题