0

I'm trying to let users favorite a project. I'm storing these projects at 2 places so I have to update them simultaneously. After looking at the firebase docs, using transactions seemed to be the best option. .

Function to toggle the favorite status:

function toggleFavorite (projectReference, uid) {
    projectReference.transaction(function(project) {
        console.log('Before-Favorites :' + project.favoriteCount);
        if (project.favorites && project.favorites[uid]) {
          project.favoriteCount--;
          project.favorites[uid] = null;
        } else {
            project.favoriteCount++;
            if(!project.favorites) {
                project.favorites= {};
            }
            project.favorites[uid] = true;
        }
        console.log(' After-Favorites :' + project.favoriteCount);
        return project;
      });
};

Function to add the eventListeners to the projects:

function AddToFavorite (uid, authorId) {
    const favoriteList = document.querySelectorAll('.btnFavorite');
    for(var i = 0; i<favoriteList.length; i++) {
        favoriteList[i].addEventListener('click', function(event) {
            const projectId = this.dataset.id;
            console.log(projectId);
            const globalProjectRef = firebase.database().ref('/projects/' + projectId);
            const userProjectRef = firebase.database().ref('/user-projects/' + authorId + '/' + projectId);
            toggleFavorite(globalProjectRef,uid);
            toggleFavorite(userProjectRef,uid);
        });
    }
}

I want to store the uid of the current user under a 'favorites' node within the project location.

When i want to store the data I can see it appearing in the database but removing it after instantly. Followed by that i get an error in the console that my project object is null. What's the best way of solving this issue ?

jordvand9
  • 159
  • 2
  • 6
  • Updating to separate locations in the database with a transaction is likely to lead to contention as the usage of your application grows. More likely you'll want to use [multi-location writes](https://firebase.googleblog.com/2015/09/introducing-multi-location-updates-and_86.html) combined with [security rules](https://stackoverflow.com/a/37956590), to [update the locations in one write operation](https://firebase.googleblog.com/2015/10/client-side-fan-out-for-data-consistency_73.html) without running into data consistency problems. – Frank van Puffelen Dec 03 '17 at 20:57
  • The behavior you're describing sounds like your server-side security rules are rejecting the write operation, either because the data isn't valid, or because you don't have permission to write it. Check your security rules, and the developer console of your browser. – Frank van Puffelen Dec 03 '17 at 20:58
  • I've tested it with putting the security rules of read & write to just true and I keep getting the same result. Is it a good idea to use update instead of transactions for what I'm trying to achieve ? – jordvand9 Dec 04 '17 at 07:56
  • For multi-location updates it is more scalable to use `update()` instead of `transaction`. I didn't realize you were using the latter, which might explain: your code doesn't seem to handle the case where `project` is `null`. When you use Firebase transactions you should always handle those, since that is a common value to be passed in for `project`. – Frank van Puffelen Dec 04 '17 at 14:48
  • Checking for null values solved it. However, the transaction increments my counter 3 now ? – jordvand9 Dec 05 '17 at 01:16
  • That problem is solved now because I had multiple event handlers calling the function at the same time. – jordvand9 Dec 05 '17 at 23:35
  • Good to hear. Happy hacking! – Frank van Puffelen Dec 06 '17 at 00:15

0 Answers0