Faruk
Faruk

Reputation: 853

Firebase onwrite function only changes values once

i deployed a simple firebase function like below which i learned from google developers. its' simple purpose is whenever someone writes 1111 to "value" field in database change it to some text but my problem is this function only changes the value once when the data field is created, after creation it does nothing so for example if a user enters 1111 it changes but if the user enters 2222 and then 1111 nothing happens. what should i change in my code?

Thanks in advance.

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

exports.sanitizePost=functions.database
    .ref('/users/{uid}')
    .onWrite(event =>{
        const post =event.data.val()
        if(post.sanitized){
            return
        }
        post.sanitized=true        
        post.value=sanitize(post.value)
        const promise = event.data.ref.set(post)
        return promise
    })
function sanitize(s){
    var sanitizedText = s
    sanitizedText = sanitizedText.replace("1111", "Congratz, you won!")
    return sanitizedText
}

Also added a function to my java client side which changes sanitized to false after database change.

my java class

import android.content.Context;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import static com.facebook.FacebookSdk.getApplicationContext;


public class database{

    private DatabaseReference mDatabase;
    private FirebaseDatabase database;


    public void kullanicikontrol(String kullaniciadi,DatabaseReference mDatabase,String value){


        mDatabase.child("users").child(kullaniciadi).child("value").setValue(value);
        mDatabase.child("users").child(kullaniciadi).child("sanitized").setValue("false");


        Context context = getApplicationContext();
        CharSequence text = kullaniciadi;
        int duration = Toast.LENGTH_LONG;

        Toast toast = Toast.makeText(context, text, duration);
        toast.show();

    }
    public void kullanicikontrol(String kullaniciadi,DatabaseReference mDatabase){


        mDatabase.child("users").child(kullaniciadi).child("status").setValue("no");
    }
}

Upvotes: 0

Views: 580

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600141

It's fairly simple. You're

if(post.sanitized){
    return
}

And then setting:

post.sanitized=true   

So when you make a change to the post, it already has been sanitized and thus simply returns from that first statement.

The easiest way to make it retrigger is to remove that check. But that will lead to a constant retrigger, since you're writing data, which will trigger the function again. Essentially the Cloud Functions variant of an infinite loop. With these type of triggered functions it is really important that you make sure you have a clear definition of when you're done.

In this case, you could define "done" as: there is no further sanitization to be done:

exports.sanitizePost=functions.database
.ref('/users/{uid}')
.onWrite(event =>{
    const post =event.data.val()
    const oldValue = post.value
    post.value=sanitize(post.value)
    if (post.value !== oldValue) {
        const promise = event.data.ref.set(post)
        return promise
    }
    else {
        return
    }
})

Upvotes: 2

Related Questions