简体   繁体   中英

Firebase JS http function is returning the incorrect response received from an axios API GET call

I have written the following HTTP firebase JS function which is returning the incorrect status 500 error response using Postman even though the axios GET call response from the API service has returned the correct 200 status response (confirmed by the console output screenshot below)

在此处输入图片说明

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
        })
      });
  });
});

In Postman I'm expecting to see "Status: 200" response, but I get this:

在此处输入图片说明

There is no error report in the Firebase console other than this:

在此处输入图片说明

As explained in the Express 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.

Following the "debugging" we did through the comments/chat, it seems that the

{message: response}

object that you pass to json() generates the error.


Following the HTTP Cloud Functions documentation , which states:

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() .

and since you explained in the chat that you "only need to return the status code" and that you "want to save the json data to: admin.database().ref(/venue-menus/${locationId}/menu) ",

I would advise you do as follows:

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
                })
            })
    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM