Linked Questions

Popular Questions

I'm new to Firebase. I'm making an application which search movies and saves them in the Real Time Database. The data to save follows the below structure:

var movieData = {
    title: "Frozen",
    genre: "Animation"
};

I save the movie this way:

database.ref().child("movies").push(movieData);

This works OK. The problem arises if I try to save it twice so, in my database I'd have the movie duplicated:

{
  "movies" : {
    "-LYSENT90rjBj_fIAHpY" : {
      "genre" : "Animation",
      "title" : "Frozen"
    },
    "-LYSEPufvX3p3w-yXf4d" : {
      "genre" : "Animation",
      "title" : "Frozen"
    }
  }
}

I'm trying to create a validation rule that checks if some movie with the same title already exists. My last attempt is this:

{
  "rules": {
    ".read": true,
    ".write": true,
    "movies": {
      "$pushid" : {
        ".validate": "root.child('movies/' + newData.child('title').val()).val() === null"
      }
    }
  }
}

I find hard to understand how it checks for all movies if the pushidis different for each movie. Moreover, in the simulator, I have seen that I can only test read, set and update methods.

Is there some way to achieve this? Or would be easier to manually check through a query in the client just before pushing the movie?

Related Questions