Reputation: 719
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.
Upvotes: 0
Views: 63
Reputation: 317487
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" })
})
}
Upvotes: 2