0

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.

1
  • 1
    if your main concern is exposing URLs you can use functions.https.onCall() instead of onRequest. That way you only need to call firebase.functions().httpsCallable("functionName"); which means no URL Commented Dec 8, 2019 at 18:58

1 Answer 1

2

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" })
      })
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.