title: Nested HTTP requests in Firebase cloud function
body: <p>I'm using an HTTP-triggered Firebase cloud function to make an HTTP request. I get back an array of results (events from Meetup.com), and I push each result to the Firebase realtime database. But for each result, I also need to make another HTTP request for one additional piece of information (the category of the group hosting the event) to fold into the data I'm pushing to the database for that event. Those nested requests cause the cloud function to crash with an error that I can't make sense of.</p>
  
  <pre><code>const functions = require("firebase-functions");
  const admin = require("firebase-admin");
  admin.initializeApp();
  
  const request = require('request');
  
  exports.foo = functions.https.onRequest(
      (req, res) =&gt; {
          var ref = admin.database().ref("/foo");
          var options = {
              url: "https://api.meetup.com/2/open_events?sign=true&amp;photo-host=public&amp;lat=39.747988&amp;lon=-104.994945&amp;page=20&amp;key=****",
              json: true
          };
          return request(
              options,
              (error, response, body) =&gt; {
                  if (error) {
                      console.log(JSON.stringify(error));
                      return res.status(500).end();
                  }
                  if ("results" in body) {
                      for (var i = 0; i &lt; body.results.length; i++) {
                          var result = body.results[i];
                          if ("name" in result &amp;&amp;
                              "description" in result &amp;&amp;
                              "group" in result &amp;&amp;
                              "urlname" in result.group
                          ) {
                              var groupOptions = {
                                  url: "https://api.meetup.com/" + result.group.urlname + "?sign=true&amp;photo-host=public&amp;key=****",
                                  json: true
                              };
                              var categoryResult = request(
                                  groupOptions,
                                  (groupError, groupResponse, groupBody) =&gt; {
                                      if (groupError) {
                                          console.log(JSON.stringify(error));
                                          return null;
                                      }
                                      if ("category" in groupBody &amp;&amp;
                                          "name" in groupBody.category
                                      ) {
                                          return groupBody.category.name;
                                      }
                                      return null;
                                  }
                              );
                              if (categoryResult) {
                                  var event = {
                                      name: result.name,
                                      description: result.description,
                                      category: categoryResult
                                  };
                                  ref.push(event);
                              }
                          }
                      }
                      return res.status(200).send("processed events");
                  } else {
                      return res.status(500).end();
                  }
              }
          );
      }
  );
  </code></pre>
  
  <p>The function crashes, log says:</p>
  
  <pre><code>Error: Reference.push failed: first argument contains a function in property 'foo.category.domain._events.error' with contents = function (err) {
        if (functionExecutionFinished) {
          logDebug('Ignoring exception from a finished function');
        } else {
          functionExecutionFinished = true;
          logAndSendError(err, res);
        }
      }
      at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1436:15)
      at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
      at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
      at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
      at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
      at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
      at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
      at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1479:13
      at Object.forEach (/user_code/node_modules/firebase-admin/node_modules/@firebase/util/dist/index.node.cjs.js:837:13)
      at validateFirebaseData (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:1462:14)
  </code></pre>
  
  <p>If I leave out the bit for getting the group category, the rest of the code works fine (just writing the name and description for each event to the database, no nested requests). So what's the right way to do this?</p>