Send a POST request by axios with firebase functions

Viewed 160

I am trying to send a POST request via axios andfirebase cloud function while the body of the request in axios contains data from changes occured in realtime db using the functions but everytime I deploy my code I get the error:

Function failed on loading user code. This is likely due to a bug in the user code. 
Error message: Error: please examine your function logs to see the error cause: 
https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs. Additional 
troubleshooting documentation can be found at 
https://cloud.google.com/functions/docs/troubleshooting#logging. Please visit 
https://cloud.google.com/functions/docs/troubleshooting for in-depth troubleshooting 
documentation.

I am quite new to firebase functions so I really don't know what I am doing here even after having some research. Code:

const functions = require("firebase-functions");
const axios = require("axios");



exports.newNodeDetected = functions.database
    .ref("Orders/{userId}/{customerId}/{ordernum}/customername")
    .onCreate((snapchot, context) => {
      const order = snapchot.val();
      const userId = context.params.userId;
      console.log(userId + "AND CUS NAME IS" + order);

      axios.post("http://something.com/data.php", {
        username: userId,
        title: "ttl",
        message: "msg",
      })
          .then(function(response) {
            console.log(response);
          })
          .catch(function(error) {
            console.log(error);
          });
    });
1 Answers

Just in case anyone had the same issue. I realized I was running my npm commands outside the functions folder, therefore the package.json wasn't getting edited and my libraries were not really getting downloaded.

Always be careful to use npm inside the functions folder!!

Related