javascript - 错误:使用 Firebase 云功能时已超过最后期限
问题描述
我写了一个小的联系表格,它调用了一个 Firebase 云函数来存储对云 Firestore 的请求。一切正常,除了 60 秒后网站抛出以下错误:
Error: deadline-exceeded
这是我的云功能:
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 方法获得了一个联系表单反应组件。
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);
});
}
几天以来我一直在遇到这个问题,我不知道它是从哪里来的:(
解决方案
它是来自 React Hooks 的“useEffect”吗?
如果是,则每次渲染都会完成您的 firebase init(在 useEffect 中)。
如果您希望 useEffect 只应用一次(如 componentDidMount),您应该在第二个参数中传递一个空数组。
useEffect(() => {
if (!firebase.apps.length) {
firebase.initializeApp(firebaseConfig)
} else {
console.log(firebase.apps)
}
}, []);
推荐阅读
- python - 字符串替换为多个项目
- bash - Linux shell 脚本,按输入字符串搜索
- arrays - Scala 加入数组并减少值
- intellij-idea - 用intellij idea vim移动光标
- traefik - 另一个网站链接不重定向 https
- wordpress - 尝试提交我的网站即时文章时出错
- vb.net - 将访问报告导出为 PDF - 无法识别的常量
- javascript - 我的函数适用于增强的 for 循环,但不适用于普通的 for 循环
- ios - 如何在 iOS 11 中将 JPEG/RAW 图像数据保存到相机胶卷,无法访问处理后的图像数据
- c# - 如何使用 iTextSharp 在单行文本中添加多种字体