Firebase Cloud Function being called twice

0

I have the following function:

'use strict';

const functions = require('firebase-functions');
const admin = require('firebase-admin');

exports.myFunction = functions.database.ref('/my_node/{myId}/status').onCreate(event => {
  const data = event.data.val();
  if (data == 'STATE01') {
    return event.data.ref.set('STATE02');
  }
});

I'm using onCreate , so the function should only be called once, not twice. Why is this happening?

See the logs:

  

9: 29: 27.975 PM myFunction Function execution took 33 ms, finished with status: 'ok'

     

9: 29: 27.943 PM myFunction Function execution started

     

9: 29: 27.875 PM myFunction Function execution took 1059 ms, finished with status: 'ok'

     

9: 29: 26.818 PM myFunction Function execution started

    
asked by anonymous 03.08.2017 / 00:28

1 answer

0

That's because you're making another set() in your function. And onCreate is executed whenever a set happens. To avoid second execution, you can replace set with update :

exports.myFunction = functions.database.ref('/my_node/{myId}/status').onCreate(event => {
  const data = event.data.val();
  if (data == 'STATE01') {
    return event.data.ref.parent.update({status:'STATE02'});
  }
});
    
02.02.2018 / 01:11