我试图使用一个函数两次,但仅使用不同的参数,以使我的代码库保持DRY状态。关键是我希望使用两个不同的URL来执行完全相同的操作,只是使用不同的Firestore集合。我总是可以为此使用POST负载,但我希望一个URL公开,而另一个URL仅供内部使用。
基本上应该是这样的:
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" })
)
}
createRestaurant.js如下所示:
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."
});
}
});
};
当我尝试上传函数时,我从firebase CLI收到此错误:
Parsing error: Identifier 'createRestaurant' has already been declared
要完成此工作我需要做什么?我觉得这可能只是JavaScript内容,而不是Firebase特有的,但我不确定。
答案 0 :(得分:1)
您错误地声明了导出的函数。应该是这样的:
session.parameters["filter"]?.get(0)?.toString()