FetchError: invalid json response body
FetchError: invalid json response body
我正在使用 node-fetch 将一些 json 数据发送到 IFTTT 休息点。数据已成功发送到端点,但我的 NodeJS 控制台出现错误。如您所见,它 returns 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 request有关系吗?
糟糕,我忘了 return
,还有 IFTTT returns text()
,不是 json。
.then(function (res) {
return res.text();
})
我正在使用 node-fetch 将一些 json 数据发送到 IFTTT 休息点。数据已成功发送到端点,但我的 NodeJS 控制台出现错误。如您所见,它 returns 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 request有关系吗?
糟糕,我忘了 return
,还有 IFTTT returns text()
,不是 json。
.then(function (res) {
return res.text();
})