亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號