gpt4 book ai didi

javascript - Firebase JS http 函数返回从 axios API GET 调用收到的错误响应

转载 作者:行者123 更新时间:2023-12-02 21:22:42 25 4
gpt4 key购买 nike

我编写了以下 HTTP firebase JS 函数,该函数使用 Postman 返回错误的状态 500 错误响应,即使来自 API 服务的 axios GET 调用响应已返回正确的 200 状态响应(由下面的控制台输出屏幕截图确认)

enter image description here

exports.doshiiMenuUpdatedWebhook = functions.https.onRequest((req, res) => {

if (req.method === 'PUT') {
return res.status(403).send('Forbidden!');
}

return cors(req, res, () => {

let verify = req.query.verify;

if (!verify) {

verify = req.body.verify;

}

let locationId = req.body.data.locationId
let posId = req.body.data.posId
let type = req.body.data.type
let uri = req.body.data.uri
let itemUri = req.body.data.itemUri

console.log('locationId', locationId);
console.log('posId', posId);
console.log('type', type);
console.log('uri', uri);
console.log('itemUri', itemUri);

const options = {
headers: {'authorization': 'Bearer ' + req.query.verify}
};

return axios.get(uri, options)
.then(response => {

console.log('response data: ', response.data);
console.log('response status: ', response.status);
console.log('response statusText: ', response.statusText);
console.log('response headers: ', response.headers);
console.log('response config: ', response.config);

return res.status(200).json({
message: response
})
})
.catch(err => {
return res.status(500).json({
error: err
})
});
});
});

在 Postman 中,我希望看到“Status: 200”响应,但我得到了:

enter image description here

Firebase 控制台中没有除此之外的错误报告:

enter image description here

最佳答案

正如快报中所解释的documentation :

res.json([body])

Sends a JSON response. This method sends a response (with the correct content-type) that is the parameter converted to a JSON string using JSON.stringify().

The parameter can be any JSON type, including object, array, string, Boolean, number, or null, and you can also use it to convert other values to JSON.

经过我们通过评论/聊天进行的“调试”,似乎

{message: response}

传递给 json() 的对象会生成错误。

<小时/>

遵循HTTP Cloud Functions documentation ,其中指出:

Important: Make sure that all HTTP functions terminate properly. By terminating functions correctly, you can avoid excessive charges from functions that run for too long. Terminate HTTP functions with res.redirect(), res.send(), or res.end().

并且由于您在聊天中解释说您“只需要返回状态代码”并且您“希望将 json 数据保存到:admin.database().ref(/venue-menus/$ {locationId}/菜单)",

我建议您执行以下操作:

exports.doshiiMenuUpdatedWebhook = functions.https.onRequest((req, res) => {

if (req.method === 'PUT') {
return res.status(403).send('Forbidden!');
}

cors(req, res, () => {

let verify = req.query.verify;

if (!verify) {
verify = req.body.verify;
}

let locationId = req.body.data.locationId
let posId = req.body.data.posId
let type = req.body.data.type
let uri = req.body.data.uri
let itemUri = req.body.data.itemUri

const options = {
headers: { 'authorization': 'Bearer ' + req.query.verify }
};

axios.get(uri, options)
.then(response => {
console.log('response data: ', response.data);
return admin.database().ref(`/venue-menus/${locationId}/menu`).set(response.data)

})
.then(response => {
return res.status(200).end()
})
.catch(err => {
return res.status(500).send({
error: err
})
})
})
});

关于javascript - Firebase JS http 函数返回从 axios API GET 调用收到的错误响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60808572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com