【发布时间】:2021-04-04 06:41:47
【问题描述】:
我想在每天凌晨 12:00 重置我的 Firebase 实时数据库中的特定值。为此,我使用 Firebase Admin SDK 更改 Firebase 实时数据库和 Cloud Functions 中的数据,以在每天凌晨 12:00 触发更改。
这是我的 Firebase 实时数据库的示例结构:
{
"users": {
"fa54487d9cbb4214b00db80e2118e4e6": {
"daily": 10
}
}
}
这是我index.js中的代码:
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
var functions = require('firebase-functions');
// The Firebase Admin SDK to access Cloud Firestore.
var admin = require('firebase-admin');
// Fetch the service account key JSON file contents
var serviceAccount = require("serviceAccountKey.json");
// Initialize the app with a service account, granting admin privileges
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databaseName.firebaseio.com"
});
// As an admin, the app has access to read and write all data, regardless of Security Rules
var db = admin.database();
var ref = db.ref("users");
// Reset today GHG emissions at 12:00 AM everyday
exports.dailyReset = functions.pubsub.schedule('0 0 * * *').onRun((context) => {
usersRef.child("{userId}").set({
daily: 0
});
});
部署错误:
! functions[dailyReset(us-central1)]: 部署错误。
函数加载用户代码失败。这可能是由于用户代码中的错误。错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs。可以在https://cloud.google.com/functions/docs/troubleshooting#logging 找到其他故障排除文档。请访问https://cloud.google.com/functions/docs/troubleshooting 获取深入的故障排除文档。
Firebase 控制台功能日志:
错误:函数终止。建议的操作:检查日志以了解终止原因。
其他故障排除文档可以在https://cloud.google.com/functions/docs/troubleshooting#logging 找到函数无法初始化。
{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"函数加载用户代码失败。这是可能是由于用户代码中的错误。
错误消息:错误:请检查您的函数日志以查看错误原因:https://cloud.google.com/functions/docs/monitoring/logging#viewing_logs。
可以在https://cloud.google.com/functions/docs/troubleshooting#logging 找到其他故障排除文档。
当我使用 firebase deploy 时,脚本不会部署,因为我的函数给了我一个错误。谁能告诉我如何修复我的代码?
【问题讨论】:
-
错误信息告诉你去哪里找到控制台中的实际错误。
-
@DougStevenson 您是在谈论来自 Firebase 控制台的日志错误消息吗?如果你是,我现在就在我的帖子中包含了它。
标签: javascript node.js firebase firebase-realtime-database google-cloud-functions