从Firebase Cloud函数调用函数

问题描述

可能这是一个简单的问题,但我不知道该怎么做。 我想从Firebase Cloud Functions中的另一个函数调用一个函数

exports.firstFunction = functions.https.onCall((params,context) => {
  return new Promise((resolve,reject) => {
    return secondFunction()  // how can I call secondFunction?
      .then((resp) => resolve(resp))
      .catch((e) => reject(e));
  });
});

exports.secondFunction = functions.https.onCall((params,reject) => {
    return resolve("secondFunction");
  });
});

使httpCallable("secondFunction")返回正确的字符串。如果我做httpCallable("firstFunction")我有一个[Error: INTERNAL]

我该怎么做?

解决方法

看看:

exports.firstFunction = functions.https.onCall((params,context) => {
  return new Promise((resolve,reject) => {
    return secondFunctionHandler()
      .then((resp) => resolve(resp))
      .catch((e) => reject(e));
  });
});

const secondFunctionHandler = (params,reject) => {
    return resolve("secondFunction");
  });
};

exports.secondFunction = functions.https.onCall(secondFunctionHandler);

我喜欢将我所有的云功能分成“处理程序”(在单独的文件中编写,然后导入)和索引文件中的单行,而不仅仅是我想重用的那些。它发现它使代码更易于阅读和管理。