0

I'm building a like/dislike system for posts on my website. Every time the user clicks on the like button, the number of likes gets updated in the database. The object stored in the database has a url of the image, a category and the number of likes as its keys.

Following is my update code: likedButton(img) is a function which is called when the user clicks the like button.

 likedButton(img) {
    var url = img.src;
    var x = this.db.list('/userPosts',

  ref => ref.orderByChild('url').equalTo(url));

  x.valueChanges().subscribe(

    (datas) => { 
      var likes = datas[0]['likes'];
      likes++;
      this.db.object('/userPosts/'+datas[0]['url']).set({
        'likes' : likes
      })
     console.log(likes);
     });
    }

I'm getting the following error: Reference.child failed: First argument was an invalid path = "/userPosts/https://firebasestorage.googleapis.com/v0/b/imageupload-d68a0.appspot.com/o/angfire2store%2Fpic488482?alt=media&token=be196b87-aa7e-4ed4-8092-a84845b885be". Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"

Please help!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Nikita
  • 211
  • 4
  • 16
  • The URL you are trying to use in the path in the database contains characters that are not allowed in such a path (`.`, `#`, `$`, `[` and `]`). You'll want to escape the URL to use it as a path. See https://stackoverflow.com/a/19148116/209103, https://stackoverflow.com/q/41764317/209103 – Frank van Puffelen Oct 06 '18 at 19:44
  • @FrankvanPuffelen the code doesn't work even when I'm using the key. :( – Nikita Oct 07 '18 at 07:27

0 Answers0