首页 > 解决方案 > 有没有办法在创建 Firebase 云函数的同一文件中调用非云函数?

问题描述

我仍然无法理解这些功能通常是如何工作的。对于该项目,我创建了一个函数,只要调用该函数 URL,该函数就会被调用。之后,被调用的函数也使用了一个效用函数。这个函数被多次使用,所以我只发现在一个单独的函数中创建它们是明智的,尽管每当我在部署后使用这个函数时,我都会收到这个错误。

TypeError: this.isMM is not a function
    at exports.addOrder.functions.https.onRequest (/user_code/index.js:130:11)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
    at /var/tmp/worker/worker.js:783:7
    at /var/tmp/worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickDomainCallback (internal/process/next_tick.js:128:9)

我只有一个文件,index.js其中放置了所有函数,尽管我不确定为什么isMM()没有读取上述函数。下面是相关的代码块(为了简单起见,我已经替换了函数的内容。

exports.addOrder = functions.https.onRequest((req, res) => {
  var newValue = req.query.my_info;
  var anotherValue = req.query.another_info;

  var my_info = this.isMM(newValue);
  if(my_info){
    var another_info = this.isMM(anotherValue);
    if(another_info){
      doThis();
    }
  }
});

isMM = (toCheck) => {
  if(toCheck === 'someString'){
    return true;
  }
  return false;
}

我希望在 addOrder 中使 isMM 可读,因为这将是我需要为这个项目做的重复发生的事情。

标签: javascriptnode.jsfirebasegoogle-cloud-functions

解决方案


只需执行以下操作,不要使用this.

  var my_info = isMM(newValue);
  if(my_info){
    var another_info = isMM(anotherValue);
    if(another_info){
      doThis();
    }
  }

推荐阅读