谢谢我正在部署一个函数来限制push命令生成的子节点。我关注的链接code to limit child node现在我只是编辑这段代码,把我的自定义代码如下链接:
'use strict';
const functions = require('firebase-functions');
// Max number of lines of the chat history.
const MAX_LOG_COUNT = 1;
// Removes siblings of the node that element that triggered the function if there are more than MAX_LOG_COUNT.
// In this example we'll keep the max number of chat message history to MAX_LOG_COUNT.
exports.truncate = functions.database.ref('/grid/{pushId}').onWrite((change) => {
const parentRef = change.after.ref.parent;
return parentRef.once('value').then((snapshot) => {
if (snapshot.numChildren() >= MAX_LOG_COUNT) {
let childCount = 0;
const updates = {};
snapshot.forEach((child) => {
if (++childCount <= snapshot.numChildren() - MAX_LOG_COUNT) {
updates[child.key] = null;
}
});
// Update the parent. This effectively removes the extra children.
return parentRef.update(updates);
}
return null;
});
});
所以在这里我部署了限制子项的函数,但是在firebase日志中得到错误:
TypeError: Cannot read property 'ref' of undefined
at exports.truncate.functions.database.ref.onWrite (/user_code/index.js:11:33)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
at /var/tmp/worker/worker.js:700:26
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
答案 0 :(得分:0)
不确定是否仍需要此功能。您是否已初始化Firebase App?添加以下行(如果尚未添加),然后再次检查。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//Define your trigger