0

I need to nest some promises.

I first need to increment a ref using: .set(admin.database.ServerValue.increment(1))

Once that is done I need to update some data at a different ref: return admin.database().ref('/user_credits/' + creditId + '/' + userId).set({ "joined_date": receivedTimeStamp, "timeStamp": receivedTimeStamp, "credits_count": 1 })

The below code works and both refs update as expected but I get a warning of:

enter image description here

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

exports.updateViewsCtAtPostsRef = functions.https.onRequest((request, response) => {

    const currentTimeStamp = Date.now();
    const receivedTimeStamp = admin.database.ServerValue.TIMESTAMP;

    const creditId = "sample_123";
    const userId = "userId_xyz";
    const gameId = "game_abc";
    admin.database().ref('user_credits').child(creditId).child(userId).once('value', snapshot => {

        if (!snapshot.exists()) {

            admin.database().ref('/games/' + gameId + '/' + 'score').set(admin.database.ServerValue.increment(1))
            .then(() => { 

                return admin.database().ref('/user_credits/' + creditId + '/' + userId).set({ "joined_date": receivedTimeStamp, "timeStamp": receivedTimeStamp, "credits_count": 1 })
                .then(() => { 
                    return true; 
                })
                .catch(error);
            })
            .catch(error);

        } else {

            const previousTimeStamp = snapshot.child("timeStamp").val();
            const creditsCount = snapshot.child("credits_count").val();

            if (previousTimeStamp + whatever) < currentTimeStamp {

                let updatedCount = creditsCount + 1;

                admin.database().ref('/games/' + gameId + '/' + 'score').set(admin.database.ServerValue.increment(1))
                .then(() => {

                    return admin.database().ref('/user_credits/' + creditId + '/' + userId).update({ "timeStamp": receivedTimeStamp, "credits_count": updatedCount })
                    .then(() => { 
                        return true; 
                    })
                    .catch(error);
                })
                .catch(error);

            } else {

               return true
            }
        }
    });
});
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • The warning message is telling you that you want to **avoid** nesting promises, as it's an anti-pattern in JS that leads to bugs. – Doug Stevenson Nov 06 '20 at 23:30
  • Ahhh ok, I'm new to Javascript, I know very little. I thought it was like Swift where I can nest callbacks. Thanks for the info and I'll look at the links. – Lance Samaria Nov 06 '20 at 23:31
  • You can nest callbacks, but it's considered a bad idea. Either rewrite to avoid nesting, or disable the eslint warning message and make sure you do it correctly. – Doug Stevenson Nov 06 '20 at 23:35
  • Is is a good or bad idea to just do both updates together? For example the **Server.increment(1)** first and then the line right underneath of it add ***return ... update the other ref***? Basically one update on top of the other. Of course I won’t know if the first one succeeded or not – Lance Samaria Nov 06 '20 at 23:39
  • I don't have an opinion - if it works the way you want, then it's a good idea. If you have a new question, post it separately, but be aware that asking for opinions is off-topic for Stack Overflow. – Doug Stevenson Nov 06 '20 at 23:40
  • Thanks, much appreciated!!! Enjoy your weekend. – Lance Samaria Nov 06 '20 at 23:42

0 Answers0