簡體   English   中英

Firebase Cloud Functions 模擬器不更新代碼

[英]Firebase emulator for Cloud Functions not updating the code

我正在使用firebase deploy --only functions部署以下雲 function:

export const testFunction = functions.https.onCall((data, context) => {
  return {"data": "hello"};
});

以及何時使用代碼從客戶端應用程序調用它

var testFunction = firebase.functions().httpsCallable("testFunction");
      testFunction().then((result) => {
        // Read result of the Cloud Function.
        this.data = result.data;
});

它按預期工作。 現在,我想繼續開發 function 在本地模擬器上測試它,所以按照文檔,我將這一行添加到 web 應用程序代碼中(在函數之前)

firebase.functions().useEmulator("localhost", 5001); // for local simulator

然后我運行本地模擬器:

firebase emulators:start --only functions

如果我現在運行客戶端應用程序,我會正確地看到調用通過本地模擬器而不是遠程雲 function。

問題:如果修改代碼,本地 function 不會更新。 我需要再次運行 firebase deploy 才能看到響應的變化。 我怎樣才能在本地部署?

這個計算器解決了這個問題。

解決方案簡而言之:

  • 每次更改代碼后,運行npm run build以重新編譯代碼。

  • 為代碼中的每個更改自動編譯。 package.json文件中的"build": "tsc"更改為"build": "tsc -w" ,然后在另一個終端 window 中運行模擬器。

您可以使用此處文檔中所述的函數 shell 來在本地部署、運行和測試函數。 我發現文檔很混亂,因為它只提到如何使用 .sh 腳本在本地進行測試。

firebase functions:shell

請注意,如果您使用 shell,則無需單獨運行模擬器; shell 將為您運行它們。 shell 將根據對 index.js 函數文件的更改自動更新,但如果您使用的是 TypeScript,您可能需要在更改后重建。 這通常是通過進入函數目錄並運行npm run build來完成的。 根據這個答案,您也可以將其自動化,但我還沒有嘗試過這種方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM