【问题标题】:Firebase Functions only returning nullFirebase 函数仅返回 null
【发布时间】:2018-10-16 01:47:14
【问题描述】:

这样做的目的是让我从我的数据库中获取所有日期,将它们从最新到最旧进行组织,按顺序从我的数据库中获取所需的信息并发送给客户端。该代码在服务器端工作,所有信息都是正确的。我只需要将它发送给我的客户。我的客户端接收字符串和我发送的任何内容,我认为问题在于我的返回语句在哪里。提前感谢任何试图帮助我的人。

这是我的服务器端代码:

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);
    }
});

【问题讨论】:

  • 使用可调用函数,您需要返回一个promise,该promise 会使用要序列化并发送给客户端的信息进行解析。
  • @DougStevenson 发送我的数组的 return 语句不会这样做吗?感谢您的耐心等待。
  • 您的 return 语句在 then() 回调中,它不会传播到函数的顶层。您的函数现在实际上没有返回任何内容。
  • @DougStevenson 哦,是的!当我将 return 语句移出 then 语句时,即使 info 正在填充信息,它也会发回一个空数组。
  • 对,因为 once() 是异步的,会立即返回。您将需要操作一系列 promise 并返回最后一个用您要发送的数据解析的。

标签: node.js firebase google-cloud-functions


【解决方案1】:

感谢@DougStevenson,我终于得到了答案!

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);
}

我需要使用 Promise 将信息发送回客户端。

如果遇到此问题,我建议人们阅读一些有用的链接。

Firebase Sync, async, and promises

Promise | MDN

Example of Promises

【讨论】:

    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2020-06-03
    • 2018-11-24
    • 1970-01-01
    • 2021-03-04
    • 2019-02-27
    • 2021-08-15
    相关资源
    最近更新 更多