Martin De Simone
Martin De Simone

Reputation: 2148

Firebase function not getting called

I have a likes value in the database and i want to change the value STATE when likes is more than 10. Here is my database structure

"QUESTIONS" : {
"ENGLISH" : {
  "QUESTIONID" : 1
},
"SPANISH" : {
  "1" : {
    "ANSWER" : true,
    "LIKES" : 11,
    "MADEBY" : "lScayuKFOrOWyenYYxbL3lJ8Utt1",
    "QUESTION" : "djshsvs",
    "SPAM" : 1,
    "STATE" : "WAITING"
  },
  "2" : {
    "ANSWER" : true,
    "LIKES" : 2,
    "MADEBY" : "lScayuKFOrOWyenYYxbL3lJ8Utt1",
    "QUESTION" : "djdbdb",
    "STATE" : "WAITING"
  },
  "3" : {
    "ANSWER" : true,
    "MADEBY" : "lScayuKFOrOWyenYYxbL3lJ8Utt1",
    "MISPELLED" : 1,
    "QUESTION" : "fhki",
    "STATE" : "WAITING"
  },
  "4" : {
    "ANSWER" : true,
    "LIKES" : 1,
    "MADEBY" : "lScayuKFOrOWyenYYxbL3lJ8Utt1",
    "QUESTION" : "bkkki",
    "STATE" : "WAITING"
  },
  "QUESTIONID" : 4
}

},

My firebase function

'use strict';

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

exports.newPost = functions.database
.ref('/QUESTIONS/SPANISH/{postID}'/"LIKES")
.onWrite(function(event) {

 const likes=event.data.value;
 const id=event.params.postID;

 if(likes>10){

    return event.data.ref.parent.child('STATE').set("ACEPTED");
 }
 return false;

});

The function is deployed correctly and all but when i increment the likes nothing happens, what i am doing wrong?

Upvotes: 0

Views: 673

Answers (2)

Anastasiya Mashoshyna
Anastasiya Mashoshyna

Reputation: 89

Except of changing reference string (as suggested above), try also changing this line:

const likes=event.data.value;

to this one:

 const likes=event.data.val();

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317828

Your string for the reference doesn't look right. You have this:

'/QUESTIONS/SPANISH/{postID}'/"LIKES"

Shouldn't it be this instead?

'/QUESTIONS/SPANISH/{postID}/LIKES'

Upvotes: 1

Related Questions