簡體   English   中英

錯誤:使用 firebase 雲函數時已超過期限

[英]Error: deadline-exceeded when working with firebase cloud functions

我寫了一個小型聯系表格,調用 firebase 雲 function 將請求存儲到雲 Firestore。 一切正常,除了 60 秒后網站拋出以下錯誤:

Error: deadline-exceeded

我使用了這個參考: https://github.com/firebase/quickstart-js/blob/7d514fb4700d3a1681c47bf3e0ff0fa3d7c91910/functions/functions/index.js

這是我的雲 function:

exports.newRequest = functions.https.onCall((data, context) => {
  return admin
    .firestore()
    .collection("requests")
    .add(data)
    .then(ref => {
        console.log(`New request written. ${ref}`)
        return ref.id
    })
    .catch(err => {
      throw new functions.https.HttpsError("unknown", error.message, error)
    })
})

這是 function 調用:

const functions = firebase.functions()
    const addMessage = functions.httpsCallable(`newRequest`)
    addMessage({
      name: name,
      contact: contact,
      message: message,
      timestamp: new Date(Date.now()).toLocaleString(),
    })
      .then(result => {
        console.log(`Cloud function called successfully. Ref: ${result.data}`)
      })
      .catch(error => {
        // Getting the Error details.
        var code = error.code
        var message = error.message
        var details = error.details
        console.log(code, message, details)
      })

有人知道如何解決這個問題嗎?

編輯:

這里是雲function日志:

7:19:33.751 PM newRequest Function execution started
7:19:33.751 PM newRequest Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
7:19:33.755 PM newRequest Function execution took 5 ms, finished with status code: 204
7:19:33.896 PM newRequest Function execution started
7:19:33.896 PM newRequest Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions
7:19:34.744 PM newRequest New request written. [object Object]
7:19:34.746 PM newRequest Function execution took 851 ms, finished with status code: 200

我的詳細設置:

  • 我有一個 gatsby 頁面,我在其中初始化 firebase。
import * as firebase from "firebase/app"
const firebaseConfig = {}
useEffect(() => {
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig)
    } else {
      console.log(firebase.apps)
    }
  })
  • 我使用以下 handleSubmit 方法獲得了一個聯系表單反應組件。
import * as firebase from "firebase/app"
import "firebase/functions"

const handleSubmit = evt => {
    evt.preventDefault()
    const addMessage = firebase.functions().httpsCallable(`newRequest`)
    addMessage({
      name: name,
      contact: contact,
      message: message,
      timestamp: new Date(Date.now()).toLocaleString(),
    })
      .then(result => {
        console.log(`Cloud function called successfully. Ref: ${result.data}`)
      })
      .catch(error => {
        // Getting the Error details.
        var code = error.code
        var message = error.message
        var details = error.details
        console.log(code, message, details)
      })
    resetName()
    resetContact()
    resetMessage()
  }

這就是 chrome 開發工具所說的:

Exception: Error: deadline-exceeded at new HttpsErrorImpl (http://localhost:8000/commons.js:4409:28) at http://localhost:8000/commons.js:4715:20
code: "deadline-exceeded"
details: undefined
message: "deadline-exceeded"
stack: "Error: deadline-exceeded↵    at new HttpsErrorImpl (http://localhost:8000/commons.js:4409:28)↵    at http://localhost:8000/commons.js:4715:20"
__proto__: Error

這是 promise 創建者:

/**
 * Returns a Promise that will be rejected after the given duration.
 * The error will be of type HttpsErrorImpl.
 *
 * @param millis Number of milliseconds to wait before rejecting.
 */
function failAfter(millis) {
    return new Promise(function (_, reject) {
        setTimeout(function () {
            reject(new HttpsErrorImpl('deadline-exceeded', 'deadline-exceeded'));
        }, millis);
    });
}

幾天以來我一直在遇到這個問題,我不知道它是從哪里來的:(

是React Hooks的“ useEffect”嗎?

如果是,則在每個渲染中都完成Firebase初始化(在useEffect中)。

如果您希望useEffect僅應用一次(例如componentDidMount),則應在第二個參數中傳遞一個空數組。

useEffect(() => {
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig)
    } else {
      console.log(firebase.apps)
    }
}, []);

3年后。 我在長時間運行(例如,2 分鍾)firebase 函數時也遇到了這個錯誤,也像 OP 一樣使用 HttpsCallable

FirebaseError 函數/超出期限

我通過進行 2 處更改來修復它:

(1)在服務器上增加firebase function超時時間如下:

exports.myFunction = functions.runWith({
  timeoutSeconds: 540
}).https.onCall(async (data: any) => {
  return await myFunction(data)
})

或者

或者,您可以通過 GCP 控制台在https://console.cloud.google.com/functions/list?

單擊 function 名稱並單擊編輯,您可以在那里增加超時

(2)設置HttpsCallableOptions超時選項:

const functions: Functions = getFunctions()
const options: HttpsCallableOptions = { timeout: 530 * 1000 } // slightly less than the 540 seconds BE timeout
const callable: HttpsCallable = httpsCallable(functions, 'flowPlayerExists', options)
const promise: Promise<HttpsCallableResult> = callable({ id })

我必須進行這兩項更改才能使其正常工作!

暫無
暫無

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

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