我编写了一个小型联系表单,它调用 Firebase 云函数来将请求存储到云 Firestore。一切正常,只是 60 秒后网站抛出以下错误:
错误:超出截止日期
这是我的云功能:
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
我的详细设置:
- 我有一个 gatsby 页面,我在其中初始化 firebase。
import * as firebase from "firebase/app"
const firebaseConfig = {}
useEffect(() => {
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig)
} else {
console.log(firebase.apps)
}
})
- 我得到了一个带有以下handleSubmit方法的联系表单 react 组件。
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);
});
}
我已经遇到这个问题好几天了,我不知道它来自哪里:(
最佳答案
3年后!我在长时间运行(例如 2 分钟)的 firebase 函数上也遇到此错误,也像 OP 一样使用 HttpsCallable
FirebaseError functions/deadline-exceeded
我通过进行 2 处更改修复了该问题:
(1) 增加服务器上的 firebase 函数超时,如下所示:
exports.myFunction = functions.runWith({
timeoutSeconds: 540
}).https.onCall(async (data: any) => {
return await myFunction(data)
})
或者
或者,您可以通过 GCP 控制台手动设置 Firebase 函数超时 https://console.cloud.google.com/functions/list?project=YOUR-PROJECT
单击函数名称并单击“编辑”,您可以在那里增加超时
(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 })
我必须进行这两项更改才能使其正常工作!
关于javascript - 错误: deadline-exceeded when working with firebase cloud functions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57299291/