简体   繁体   English

TS Cloud 函数的部署/转换不起作用

[英]Deployement / Transpilation of TS Cloud functions not working

I am working on a side project using TS and Firebase CLI 11.16.1 I usually use JS only.我正在使用 TS 和 Firebase CLI 11.16.1 进行一个副项目,我通常只使用 JS。 When I created my Firebase project I opted for Typescript for the functions.当我创建我的 Firebase 项目时,我为函数选择了 Typescript。 (Then saw it was TS transpiled to JS but nevermind) I did the classic stuff: firebase init Then opted for functions and Typescript instead of Javascript. (然后看到它是 TS 转换为 JS 但没关系)我做了经典的东西: firebase init然后选择了函数和 Typescript 而不是 Javascript。

I wrote my functions without any error (in TS) in my functions/src/index.ts file.我在我的 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);
    
});

I deployed using the classical firebase deploy Then i did few changes deployed again.我使用经典的firebase deploy进行部署然后我做了一些更改再次部署。 (changing the Timezone since I want it to run at Paris time adding new updates to the database etc) (更改时区,因为我希望它在巴黎时间运行,向数据库添加新更新等)

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

And deployed again.并再次部署。 Theses changes never seemed to apply correctly.这些更改似乎从未正确应用。 Like if the functions never updated itself.就像功能从未更新过一样。 Every time I run my functions, it's still the first version.每次我运行我的功能,它仍然是第一个版本。

But the deployment logs are fine, it says functions got updated and deployed.但是部署日志很好,它说功能已更新和部署。

Then I checked the function inside cloud platform and saw a hierarchy with a lib folder.然后我检查了云平台内部的功能,看到了一个带有 lib 文件夹的层次结构。 Then checked the doc and saw it was transpiled TS code to Javascript然后查看文档,看到它是将 TS 代码转译为 Javascript 在此处输入图像描述

So my code is in src/index.ts.所以我的代码在 src/index.ts 中。 Cloud platform is supposed to convert it to Javascript, inside the lib folder;云平台应该将其转换为 Javascript,在 lib 文件夹中; Inside the lib there is an index.js, then a folder with another index.js, which are supposed to be my converted code.在 lib 中有一个 index.js,然后是一个包含另一个 index.js 的文件夹,这应该是我转换后的代码。 First thing I don't understand, why is there two files?首先我不明白,为什么有两个文件?

I checked, and the first index.js (lib/index.js) is still the first version of my code I wrote.我查了一下,第一个index.js(lib/index.js)还是我写的代码的第一个版本。 (first block) The second one (lib/functions/src/index.ts) is my most recent code. (第一块)第二块(lib/functions/src/index.ts)是我最近的代码。 (2nd block) (第二块)

When i try to deploy using CLI, everything seem to work fine.当我尝试使用 CLI 进行部署时,一切似乎都运行良好。 But then when I run my functions, manually or at 00:01 it's still the first version (the code of the lib/index.js)但是当我手动或在 00:01 运行我的函数时,它仍然是第一个版本(lib/index.js 的代码)

I tried to edit manually this lib/index.js with cloud platform and deploy, it revert back to the first version.我尝试用云平台手动编辑这个 lib/index.js 并部署,它恢复到第一个版本。

Am I the only one who got the issue?我是唯一遇到问题的人吗? My guess is: it uses the wrong functions (older) since there is one in the root of the /lib and another in lib/functions/src...我的猜测是:它使用了错误的函数(较旧的),因为 /lib 的根目录中有一个,而 lib/functions/src 中有另一个...

If I can't find a way to resolve this I will try to go back to Javascript for the cloud functions and deploying again.如果找不到解决此问题的方法,我将尝试返回 Javascript 以获取云功能并再次部署。

Don't know why the wrong version of your code is being used when you run your functions.不知道为什么在运行函数时使用了错误版本的代码。 One thing you could try is to delete the lib directory and then run firebase deploy again so This will force the Firebase CLI to transpile your TypeScript code and place the output in a new lib directory, which should contain the most recent version of your code.您可以尝试的一件事是删除lib目录,然后再次运行firebase deploy这样这将强制 Firebase CLI 转换您的 TypeScript 代码并将输出放在新的 lib 目录中,该目录应包含最新版本的代码。

You can also view the logs for your functions by running the您还可以通过运行以下命令查看函数的日志

firebase functions:log火力地堡功能:日志

command to get more information about the behavior.命令以获取有关该行为的更多信息。

You may also try to delete all the functions and recreate the function using您也可以尝试删除所有功能并使用重新创建功能

firebase init火力初始化

with giving typescript as your options and making sure your directories are named correctly as per the context.将打字稿作为您的选择,并确保您的目录根据上下文正确命名。 And deploy the function locally to verify that it is working correctly in the local environment, this can be done with firebase emulators并在本地部署该功能以验证其在本地环境中是否正常工作,这可以通过firebase 模拟器来完成

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM