[英]Deployement / Transpilation of TS Cloud functions not working
我正在使用 TS 和 Firebase CLI 11.16.1 进行一个副项目,我通常只使用 JS。 当我创建我的 Firebase 项目时,我为函数选择了 Typescript。 (然后看到它是 TS 转换为 JS 但没关系)我做了经典的东西: firebase init
然后选择了函数和 Typescript 而不是 Javascript。
我在我的 functions/src/index.ts 文件中编写了我的函数,没有任何错误(在 TS 中)。
exports.todayCode = functions.pubsub.schedule('every day at 00:01').onRun((context) => {
const fsdb = admin.firestore();
const colors = ["R", "G", "B", "Y", "P", "O"];
let code = "";
const date = formatDateWithZone(new Date(),'Europe/Paris').slice(0, 10);
//generate random code from the colors array and add it to the code string
for (let i = 0; i < 4; i++) {
code += colors[Math.floor(Math.random() * colors.length)];
}
const codeObj = {
code: code
}
//save the code in the firestore database with the date formatted YYYY-MM-DD as id
return fsdb.collection("codes").doc(date).set(codeObj);
});
我使用经典的firebase deploy
进行部署然后我做了一些更改再次部署。 (更改时区,因为我希望它在巴黎时间运行,向数据库添加新更新等)
exports.todayCode = functions.pubsub.schedule('every day at 00:01').timeZone('Europe/Paris') .onRun((context) => {
const fsdb = admin.firestore();
const colors = ["R", "G", "B", "Y", "P", "O"];
let code = "";
console.log("creating code");
const date = formatDateWithZone(new Date(),'Europe/Paris').slice(0, 10);
console.log("today date : " ,date);
//generate random code from the colors array and add it to the code string
fsdb.collection("codes").doc(date).get().then((doc: { exists: any; })=>{
if(doc.exists)
{
console.log("code existe déja !")
return true;
}
else
{
for (let i = 0; i < 4; i++) {
code += colors[Math.floor(Math.random() * colors.length)];
}
const codeObj = {
code: code,
date : new Date()
}
console.log("code created !");
//save the code in the firestore database with the date formatted YYYY-MM-DD as id
return fsdb.collection("codes").doc(date).set(codeObj);
}
});});
并再次部署。 这些更改似乎从未正确应用。 就像功能从未更新过一样。 每次我运行我的功能,它仍然是第一个版本。
但是部署日志很好,它说功能已更新和部署。
然后我检查了云平台内部的功能,看到了一个带有 lib 文件夹的层次结构。 然后查看文档,看到它是将 TS 代码转译为 Javascript
所以我的代码在 src/index.ts 中。 云平台应该将其转换为 Javascript,在 lib 文件夹中; 在 lib 中有一个 index.js,然后是一个包含另一个 index.js 的文件夹,这应该是我转换后的代码。 首先我不明白,为什么有两个文件?
我查了一下,第一个index.js(lib/index.js)还是我写的代码的第一个版本。 (第一块)第二块(lib/functions/src/index.ts)是我最近的代码。 (第二块)
当我尝试使用 CLI 进行部署时,一切似乎都运行良好。 但是当我手动或在 00:01 运行我的函数时,它仍然是第一个版本(lib/index.js 的代码)
我尝试用云平台手动编辑这个 lib/index.js 并部署,它恢复到第一个版本。
我是唯一遇到问题的人吗? 我的猜测是:它使用了错误的函数(较旧的),因为 /lib 的根目录中有一个,而 lib/functions/src 中有另一个...
如果找不到解决此问题的方法,我将尝试返回 Javascript 以获取云功能并再次部署。
不知道为什么在运行函数时使用了错误版本的代码。 您可以尝试的一件事是删除lib
目录,然后再次运行firebase deploy
这样这将强制 Firebase CLI 转换您的 TypeScript 代码并将输出放在新的 lib 目录中,该目录应包含最新版本的代码。
您还可以通过运行以下命令查看函数的日志
火力地堡功能:日志
命令以获取有关该行为的更多信息。
您也可以尝试删除所有功能并使用重新创建功能
火力初始化
将打字稿作为您的选择,并确保您的目录根据上下文正确命名。 并在本地部署该功能以验证其在本地环境中是否正常工作,这可以通过firebase 模拟器来完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.