简体   繁体   中英

Firebase Functions - use a function twice with different arguments

I'm trying to use a function twice but just with different arguments so that my codebase is kept DRY. The point is that I want two different URLs for doing the exact same thing but just with different Firestore collections. I could always use a POST payload for that but I want one URL to be exposed to the public whereas the other one is just for internal use.

It should basically be something like this:

index.js

const createRestaurant = require("./createRestaurant");

module.exports = {
  addPendingRestaurant: functions
    .region("europe-west1")
    .https.onRequest(
      createRestaurant(request, response, { collection: "restaurants_pending" })
    ),

  createRestaurant: functions
    .region("europe-west1")
    .https.onRequest(
      createRestaurant(request, response, { collection: "restaurants_v2" })
    )
}

With createRestaurant.js looking like this:

const createRestaurant = (request, response, options) => {
  cors(request, response, async () => {
    const placeId = request.body.place_id;

    console.log(`Place with id ${placeId} is about to be created`);

    try {
      const autoFill = await axios.get(url);
      const autoFillData = autoFill.data.result;

      //get data from maps
      const payload = {
        ...
      };

      if (await restaurantExists(options.collection)) {
        console.log("Document exists. Aborting.");
        return response.status(403).send({ message: "Place already exists." });
      }

      await createRestaurant(options.collection, payload);
      console.log(`Place with id ${placeId} created successfully`);
      return response
        .status(200)
        .send({ message: `Place with id ${placeId} created successfully` });
    } catch (error) {
      console.error(error);
      return response.status(400).send({
        error: "Unable to handle the request. The placeId might not be valid."
      });
    }
  });
};

When I try to upload my functions I get this error from the firebase CLI:

Parsing error: Identifier 'createRestaurant' has already been declared

What do I have to do to make this work? I feel like this might just be a JavaScript thing and not Firebase specific but I'm not sure.

You're declaring your exported functions incorrectly. It should be like this:

  module.exports = {
    addPendingRestaurant: functions
      .region("europe-west1")
      .https.onRequest((request, response) => {
        createRestaurant(request, response, { collection: "restaurants_pending" })
      }),

    createRestaurant: functions
      .region("europe-west1")
      .https.onRequest((request, response) => {
        createRestaurant(request, response, { collection: "restaurants_v2" })
      })
  }

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