Firebase云功能限制子级 [英] firebase cloud functions limit children

查看:238
本文介绍了Firebase云功能限制子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢您正在部署一个函数来限制从push命令生成的子节点.我关注的链接

Thank you I am deploying a function to limit the child nodes generated from push command.The link I am following code to limit child node Now I am just editing this code putting my custom code as follows link:

    '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日志中出现错误,如下所示:

So here I deploy the functions to limit the children but, getting error in firebase logs as:

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)

推荐答案

不确定是否仍需要此功能.您是否已初始化Firebase App?添加以下行(如果尚未添加),然后再次检查.

Not sure if you still need this. Have you initialized Firebase App? Add the following lines, if not already added, and check again.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

//Define your trigger

这篇关于Firebase云功能限制子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆