为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕沐林林 2022-05-26 14:33:17
我编写了以下 HTTP firebase JS 函数,它使用 Postman 返回了不正确的状态 500 错误响应,即使来自 API 服务的 axios GET 调用响应返回了正确的 200 状态响应(由下面的控制台输出屏幕截图确认)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        })      });  });});在邮递员中,我希望看到“状态:200”响应,但我得到了这个:Firebase 控制台中没有除此之外的错误报告:
查看完整描述

1 回答

?
神不在的星期二

TA贡献1963条经验 获得超6个赞

如 Express文档中所述:

res.json([body])

发送 JSON 响应。此方法发送一个响应(具有正确的内容类型),该响应是使用 .json 转换为 JSON 字符串的参数 JSON.stringify()

参数可以是任何 JSON 类型,包括对象、数组、字符串、布尔值、数字或 null,您也可以使用它来将其他值转换为 JSON。

在我们通过评论/聊天进行“调试”之后,似乎

{message: response}

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


遵循HTTP Cloud Functions 文档,其中指出:

重要提示:确保所有 HTTP 函数都正确终止。通过正确终止函数,您可以避免因运行时间过长的函数而产生过多费用。res.redirect()使用、res.send()或终止 HTTP 函数 res.end()

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

我建议你这样做:

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

                })

            })

    })

});


查看完整回答
反对 回复 2022-05-26
  • 1 回答
  • 0 关注
  • 174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号