firebase cloud functions limit children

2019-08-26 19:22发布

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;
  });
});

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)

1条回答
Juvenile、少年°
2楼-- · 2019-08-26 20:28

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
查看更多
登录 后发表回答