首页 > 解决方案 > 错误:使用 Firebase 云功能时已超过最后期限

问题描述

我写了一个小的联系表格,它调用了一个 Firebase 云函数来存储对云 Firestore 的请求。一切正常,除了 60 秒后网站抛出以下错误:

Error: deadline-exceeded

我使用了这个参考:https ://github.com/firebase/quickstart-js/blob/7d514fb4700d3a1681c47bf3e0ff0fa3d7c91910/functions/functions/index.js

这是我的云功能:

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

这是函数调用:

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

有人知道如何解决这个问题吗?

编辑:

这是云功能日志:

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

我的详细设置:

import * as firebase from "firebase/app"
const firebaseConfig = {}
useEffect(() => {
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig)
    } else {
      console.log(firebase.apps)
    }
  })
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

这是承诺的创造者:

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

几天以来我一直在遇到这个问题,我不知道它是从哪里来的:(

标签: javascriptnode.jsfirebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


它是来自 React Hooks 的“useEffect”吗?

如果是,则每次渲染都会完成您的 firebase init(在 useEffect 中)。

如果您希望 useEffect 只应用一次(如 componentDidMount),您应该在第二个参数中传递一个空数组。

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

推荐阅读