0

I am fetching data from a node from Firebase Database and then after doing some calculations I am updating it.

I am using Firebase SDK in node js to perform this task.

Here is the code:

app.get("/setBookedSpots", function (request, response) {
    console.log("booked spots called");

    var verifierEmail = request.query.verifierEmail;
    var toBeBookedSpot = request.query.bookSpot;

    console.log(toBeBookedSpot);

    admin.auth().getUserByEmail(verifierEmail)
        .then(function (userRecord) {
            console.log("Successfully fetched user data:", userRecord.toJSON());

            var verifierId = userRecord.uid;

            var refToUserInformation = db.ref("user/" + verifierId);

            refToUserInformation.child("Society_Name").on("value", function (snapshot) {
                console.log(snapshot.val());
                var SocietyName = snapshot.val();

                refToSocietyBookingStatus = db.ref("Societies/" + SocietyName + "/Society_Parking_Details/Society_Parking_Status");

                refToSocietyBookingStatus.child("Booked_Spots").on("value", function (snapshot) {

                    var Booked_Spots = snapshot.val();

                    console.log(Booked_Spots);
                    console.log("to be booked spot", toBeBookedSpot);

                    Booked_Spots = Booked_Spots.toString() + "," + toBeBookedSpot;

                    console.log("after booked spot", Booked_Spots);
                    refToSocietyBookingStatus.update({
                        "Booked_Spots": Booked_Spots
                    })

                    response.send({ "msg": "success" });
                })
            })
        });
});

I am getting the booked spots and the updating them based on the request parameter.

When I hit it, it goes in infinite callback. What I mean to say is it keeps on appending recursively as there is no loop which makes it to crash.

I tried using a different variable for the database reference so that it might not be calling the parent event again but still the same problem.

Here is the service call url:

http://localhost:8080/[email protected]&bookSpot=A7

2 Answers 2

1

you may use instead of response.send({ "msg": "success" }); to change this one response.send(JSON.stringify({ msg: 'success'}));

Sign up to request clarification or add additional context in comments.

3 Comments

I just tried your solution, but still infinite loop.
in your client side code use if condition check the msg.equals("success") or not..
But the problem is not regarding the response, I am getting correct response if I remove refToSocietyBookingStatus.update({ "Booked_Spots": Booked_Spots })
1

After trying a lot, I came to know that the on("value") event was being executed as I was updating the same node from which I just retrived value and thus went in recursion and kept on calling as it went on updating.

Here is the solution to that: replaced on("value") with once("value")

Making it execute just once.

1 Comment

Using on() in Cloud Functions is a pretty explicit anti-pattern, as it leads to hard to debug behavior. Good to see that you found the solution yourself!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.