MrNiceCream
MrNiceCream

Reputation: 43

Firebase Functions only returning null

The goal of this is for me to grab all the dates from my database, organize them from newest to oldest, grab the information needed from my database in that order and send that client side. The code does work server side and all the information is correct. I just need to send it to my client. My client side receives strings and anything I send, I think the problem is where my returning statement is. Thank you in advanced to whoever attempts to help me.

This is my server side code:

exports.loadNewestPlaylist = functions.https.onCall((request, response) => {
    try {
        var dates = [];
        var Info = [];
        var query = admin.database().ref().orderByKey();

        query.once("value")
            .then(function (snapshot) {
                snapshot.forEach(function (snapshot) {
                    if (dates.indexOf(snapshot.child("Date").val()) > -1) {}
                    else {
                        dates.push(snapshot.child("Date").val());
                        dates.sort(date_sort_asc);
                    }
                });

                dates.forEach(function (date) {
                    query.once("value")
                        .then(function (snapshot) {
                            snapshot.forEach(function (snapshot) {
                                if (date === snapshot.child("Date").val()) {
                                    Info.push(snapshot.child("Url").val(), snapshot.key);
                                }
                            });

                        });

                });

                return Info;

            });

        var date_sort_asc = function (date1, date2) {
            if (date1 > date2) return 1;
            if (date1 < date2) return -1;
            return 0;
        };
    }

    catch (error) {
        console.error(error);
    }
});

Upvotes: 2

Views: 578

Answers (1)

MrNiceCream
MrNiceCream

Reputation: 43

Thanks to @DougStevenson I finally got the answer!

try {
    var dates = [];
    var Info = [];
    var query = admin.database().ref().orderByKey();

    return new Promise((resolve, reject) => {

        query.once("value").then(function (snapshot) {

            snapshot.forEach(function (snapshot) {
                if (dates.indexOf(snapshot.child("Date").val()) > -1) { }
                else {
                    dates.push(snapshot.child("Date").val());
                    dates.sort(date_sort_asc);
                }
            });

            dates.forEach(function (date) {
                snapshot.forEach(function (snapshot) {
                    if (date === snapshot.child("Date").val()) {
                        Info.push(snapshot.child("Url").val(), snapshot.key);
                    }
                });
            });
            resolve(Info);
        });
    });

    loadNewestPlaylist().then(result => {
        return result;
    });

    var date_sort_asc = function (date1, date2) {
        if (date1 > date2) return 1;
        if (date1 < date2) return -1;
        return 0;
    };
}

catch (error) {
    console.error(error);
}

I needed to use promise in order to send the info back to client side.

Some helpful links I recommend for people to read if they ever run into this problem are below.

Firebase Sync, async, and promises

Promise | MDN

Example of Promises

Upvotes: 2

Related Questions