Fez Vrasta
Fez Vrasta

Reputation: 14815

Firebase Cloud Functions fail to deploy

I'm trying to deploy the two following functions:

exports.increaseWaitinglistCounters = functions.database
  .ref('waitinglists/$iid/$uid')
  .onCreate(async () => {
    await admin
      .database()
      .ref(`waitinglistCounters/$iid`)
      .transaction((count) => {
        return (count || 0) + 1;
      });
  });

exports.decreaseWaitinglistCounters = functions.database
  .ref('waitinglists/$iid/$uid')
  .onDelete(async () => {
    await admin
      .database()
      .ref(`waitinglistCounters/$iid`)
      .transaction((count) => {
        return Math.max((count || 0) - 1, 0);
      });
  });

But I keep hitting deployment errors, I looked in the Google Cloud logs but I can't find anything useful.

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {
      "code": 13,
      "message": "Failed to configure trigger providers/google.firebase.database/eventTypes/[email protected] (__gcf__.us-central1.decreaseWaitinglistCounters)"
    },
    "authenticationInfo": {
      "principalEmail": "[email protected]"
    },
    "serviceName": "cloudfunctions.googleapis.com",
    "methodName": "google.cloud.functions.v1.CloudFunctionsService.CreateFunction",
    "resourceName": "projects/xxxxxx/locations/us-central1/functions/decreaseWaitinglistCounters"
  },
  "insertId": "ilglgod1bk8",
  "resource": {
    "type": "cloud_function",
    "labels": {
      "project_id": "xxxxxx",
      "region": "us-central1",
      "function_name": "decreaseWaitinglistCounters"
    }
  },
  "timestamp": "2021-02-04T16:10:10.853578Z",
  "severity": "ERROR",
  "logName": "projects/xxxxxxx/logs/cloudaudit.googleapis.com%2Factivity",
  "operation": {
    "id": "operations/xxxxxx",
    "producer": "cloudfunctions.googleapis.com",
    "last": true
  },
  "receiveTimestamp": "2021-02-04T16:10:11.326391635Z"
}

What's wrong with my code? I'm trying to increment and decrement a counter when a node is created or removed from a parent node.

Upvotes: 0

Views: 57

Answers (1)

Louis Coulet
Louis Coulet

Reputation: 4591

The error log mentions an issue with your second function's trigger, I think you should replace the dollar sign with curly braces in your ref definition for wildcards:

ref('waitinglists/{iid}/{uid}')

The related documentation: https://firebase.google.com/docs/functions/database-events#specify_the_instance_and_path

Upvotes: 2

Related Questions