简体   繁体   English

Firebase 函数 - 使用具有不同参数的函数两次

[英]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.关键是我想要两个不同的 URL 来做完全相同的事情,但只是使用不同的 Firestore 集合。 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.我总是可以为此使用 POST 有效负载,但我希望一个 URL 向公众公开,而另一个仅供内部使用。

It should basically be something like this:它基本上应该是这样的:

index.js索引.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: 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."
      });
    }
  });
};

When I try to upload my functions I get this error from the firebase CLI:当我尝试上传我的函数时,我从 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.我觉得这可能只是 JavaScript 的事情,而不是 Firebase 特定的,但我不确定。

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" })
      })
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest 用不同的参数模拟同一个函数两次 - Jest mock the same function twice with different arguments Firebase 功能 | 如果在不同的文件中使用导入会被调用两次吗? - Firebase Functions | Does imports gets called twice if used in different files? 好的做法是在箭头函数中使用name = arguments作为函数参数? - It is good practice to use name=arguments as function arguments in arrow functions? Coffeescript-使用函数参数[2]进行方法链接或使用函数代替参数 - Coffeescript - Method chaining with function arguments[2] or use functions instead of params 如何编写一个以两次调用,以不同长度的数组作为参数,可以正确返回预期的输出数组? - How can I write a function to be called twice, with arrays of different lengths as arguments, that correctly returns expected output array? 使用不同参数执行的JavaScript函数 - JavaScript functions executed with different arguments 如何在Firebase云函数中使用javascript setTimeout函数 - how to use javascript setTimeout function in firebase cloud functions Firebase 函数错误 - firebase.functions 不是函数 - Firebase Functions Error - firebase.functions is not a function 为什么不能在不同的浏览器中使用自变量功能? - Why can't use function of arguments in the different browsers? 我可以在javascript中使用函数参数的不同方法是什么 - What are different ways i can use function arguments in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM