fetchMock:未捕获的类型错误

fetchMock: uncaught typeError

我用 fetchMock (v.5.13.1) 嘲笑了我的 API-Respones。我已经使用它很长时间了,但我还没有看到这种行为。

我模拟了两个非常相似的 GET 响应。

fetchMock.get('glob:*/shippings/',"results":[
{"id": "1234", "status": "RELEASED", "foo": "bar"},
{"id": "5678", "status": "CREATED", "foo": "bar"},)

fetchMock.get('glob:*/shipping/myId1234',
{"id": "1234", "status": "RELEASED", "foo": "bar"})

现在,第一个可以正常工作,但是第二个 returns 我得到了这个错误消息:

fetch-mock.js:187 Uncaught TypeError: Invalid status RELEASED passed on response object. To respond with a JSON object that has status as a property assign the object to body e.g. {"body": {"status: "registered"}}

我有一个假设,我不能模拟一些包含状态的响应,因为那在某种程度上是状态代码的保留属性,但我不太确定并且我无法在网上找到任何类似的错误。

对于 fetchMock 的第二个请求,它假定 status 是作为整数提供的标准代码之一。根据文档,提供给 fetchMock 的配置需要以下参数

If an object only contains properties from among those listed below it is used to configure a Response to return

body: String | Object

Set the Response body. See the non-config Object section of the docs below for behaviour when passed an Object

Server responded ok { token: 'abcdef' }

status: Integer

Set the Response status

200, 404, 503

headers: Object

Set the Response headers

{'Content-Type': 'text/html'}

redirectUrl: String

The url from which the Response should claim to originate from (to imitate followed directs). Will also set redirected: true on the response

throws: Error

Force fetch to return a Promise rejected with the value of throws

new TypeError('Failed to fetch')

但是对于自定义状态属性,您可以使用正文进行响应

fetchMock.get('glob:*/shipping/myId1234', {
    body: {"id": "1234", "status": "RELEASED", "foo": "bar"}
})