FetchError:无效的json响应正文

用户名

我正在使用node-fetch将一些json数据发送到IFTTT休息点。数据已成功发送到端点,但是我的NodeJS控制台出现错误。如您所见,它返回undefined,然后说存在无效的json响应主体。我检查了身体,对我来说看起来不错。

问题是什么?

  async function checkTemperatureRange() {
    try {
      const temperatureSettings = await getTemperatureSetting();
      const currentTemperature = await getCurrentTemperature();

      if (currentTemperature < temperatureSettings.min_temp || currentTemperature > temperatureSettings.max_temp) {
        console.log('Temp NOT in range!');
        const body = { value1: currentTemperature };
        fetch('https://maker.ifttt.com/trigger/temp_reading/with/key/abc123', {
          method: 'post',
          body:    JSON.stringify(body),
          headers: { 'Content-Type': 'application/json' },
        })
        .then(function (res) {
          res.json()
        })
        .then(function (json) {
          console.log(json)
        })
        .catch(function (err) {
          console.log('node-fetch error: ', err)
        });
      }
      else {
        console.log('Temp in range :)');
      }
    } catch(error) {
      console.error(error);
    } 
  }

错误

undefined

(node:7488) UnhandledPromiseRejectionWarning: FetchError: invalid json response body at https://maker.ifttt.com/trigger/temp_reading/with/key/abc123 reason: Unexpected token C in JSON at position 0
    at C:\Users\leke\dev\code-training-camp-demo\node_modules\node-fetch\lib\index.js:272:32
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:7488) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:7488) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

编辑

如果console.log(res)我得到

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [Object],
        readable: true,
        domain: null,
        _events: [Object],
        _eventsCount: 3,
        _maxListeners: undefined,
        _writableState: [Object],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://maker.ifttt.com/trigger/temp_reading/with/key/abc123',
     status: 400,
     statusText: 'Bad Request',
     headers: Headers { [Symbol(map)]: [Object] },
     counter: 0 } }

那与Bad请求有关吗?

用户名

糟糕,我忘记了return,而且IFTTT返回text(),而不是json。

.then(function (res) {
  return res.text();
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

响应正文上的字符无效

来自分类Dev

改造-从响应正文中删除一些无效字符,然后将其解析为json

来自分类Dev

reCaptcha无效的json响应

来自分类Dev

无效的JSON响应

来自分类Dev

AJAX:无效的JSON响应

来自分类Dev

Django无效的JSON响应

来自分类常见问题

改造2:从响应正文获取JSON

来自分类Dev

使用application / json类型响应来响应无效的JSON请求

来自分类Dev

JSON响应的SED无效命令代码

来自分类Dev

DataTables Symfony 中的 JSON 响应无效

来自分类Dev

数据表中的 JSON 响应无效

来自分类Dev

数据表抛出无效的 JSON 响应

来自分类Dev

执行正文时,npm请求未得到json响应。{object}

来自分类Dev

Google Sheets API:响应正文中的Exception vs. Json

来自分类Dev

尝试使用HttpRequestMessage发送JSON正文时的错误响应

来自分类Dev

如何正确使用POST的JSON响应正文中的数据?

来自分类Dev

Internet Explorer在响应正文中提供格式错误的JSON

来自分类Dev

双对象嵌套JSON响应正文[目标C]

来自分类Dev

响应正文JSON中的序列化日期格式

来自分类Dev

在nodjs中具有正文响应的多级json

来自分类Dev

改造2:POST JSON正文和XML响应

来自分类Dev

Docker错误:HTTP 408响应正文:无效字符'<'寻找值的开头

来自分类Dev

docker:解析HTTP 403响应正文时出错:无效字符'<'寻找值的开始

来自分类Dev

Docker错误:HTTP 408响应正文:无效字符'<'寻找值的开头

来自分类Dev

响应http响应正文的编码

来自分类Dev

从ClientResource检索响应正文

来自分类Dev

如何从Akka HTTP POST请求读取JSON正文并将最终响应作为JSON数组发送

来自分类Dev

从Spring控制器返回空JSON以获取无效响应

来自分类Dev

DataTables警告:table = userTable-无效的JSON响应?

Related 相关文章

热门标签

归档