2

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);
    }
});
MrNiceCream
  • 43
  • 1
  • 5
  • 2
    With callable functions, you need to return a promise that resolves with the information to serialize and send to the client. – Doug Stevenson Oct 12 '18 at 14:19
  • @DougStevenson Wouldn't the return statement sending my array do that? Thanks for your patience. – MrNiceCream Oct 12 '18 at 16:17
  • Your return statement is inside a then() callback, which doesn't propagate to the top level of the function. Your function is actually returning nothing right now. – Doug Stevenson Oct 12 '18 at 16:18
  • @DougStevenson Oh yes! When I move the return statement out of the then statement , it sends back a null array even though info is being filled with the information. – MrNiceCream Oct 12 '18 at 16:24
  • Right, because once() is asynchronous and returns immediately. You'll need to manipulate a series of promised and return the final one that resolves with the data you want to send. – Doug Stevenson Oct 12 '18 at 17:02

1 Answers1

2

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

MrNiceCream
  • 43
  • 1
  • 5