【问题标题】:Google firebase callable web functions - returns {data: null}Google firebase 可调用网络函数 - 返回 {data: null}
【发布时间】:2021-10-16 06:50:04
【问题描述】:

我目前正在尝试使用可调用的 web firebase 函数从 mongodb 数据库查询中返回一些数据。函数被调用。但是,fullName 函数在我的浏览器控制台中返回 {data: null}

我也尝试过return "test",它也返回了与{data: null}相同的值

我完全不确定这是为什么。当我尝试 console.log

            const data = {
                totalInfections: totalInfections,
                totalUsers: totalUsers,
                totalCommands: totalCommands,
                totalLiveInfections: totalLiveInfections,
                totalLiveInfectedChannels: totalLiveInfectedChannels
            }
            console.log(data)
            return data

我的函数日志(在我的本地主机上)或我的终端中没有任何内容。

但是,如果我转到 http://localhost:5001/pandemic-8955f/us-central1/fullName,我的函数日志中会出现错误

function[us-central1-fullName]
{
  "severity": "WARNING",
  "message": "Request has invalid method. GET"
}
function[us-central1-fullName]
{
  "severity": "ERROR",
  "message": "Invalid request, unable to process."
}

function[us-central1-fullName]
Finished "us-central1-fullName" in ~1s

我不确定为什么会发生这种情况,任何指示或建议都会非常有帮助。

exports.fullName = functions.https.onCall((lol, lol1) => {
    var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false";
    MongoClient.connect(url, function(err, db) {
        if (err) throw err;
        var dbo = db.db("discord_pandemic");
        dbo.collection("ArrayStats").find().toArray(function(err, result) {
            if (err) throw err;
            const totalInfections = result[0].total_infections
            const totalUsers = result[0].total_users
            const totalCommands = result[0].total_commands
            const totalLiveInfections = result[0].total_live_infections
            const totalLiveInfectedChannels = result[0].total_live_infected_channels
            const data = {
                totalInfections: totalInfections,
                totalUsers: totalUsers,
                totalCommands: totalCommands,
                totalLiveInfections: totalLiveInfections,
                totalLiveInfectedChannels: totalLiveInfectedChannels
            }
            console.log(data)
            return data
        });
    });
});
<body>
    <input type="text" id="fName" placeholder="First Name">
    <input type="text" id="lName" placeholder="Last Name">
    <button onClick="addName()"> Add Name </button>
    <script>
        function addName() {
            var fullName = firebase.functions().httpsCallable('fullName');
            //For the fullName we have defined that fullName takes some data as a parameter 
            fullName({}).then((result) => {
                console.log(result);
            }).catch((error) => {
                console.log(error);
            })
        }
    </script>
</body>

【问题讨论】:

    标签: javascript html firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您的代码中有异步调用,一个是打开与 Mongo 的连接,另一个是从 Mongo 加载数据。当 daa 已加载且您的 return data 执行时,您的 Function 的关闭 } 早已执行 - 没有人会看到返回。

    在大多数情况下,您可以提出通常会在启动异步操作时从您调用的 API 中获得的承诺。但似乎MongoClient.connect 没有返回承诺,所以你必须像这样构造你自己的:

    exports.fullName = functions.https.onCall((lol, lol1) => {
      return new Promise((resolve, reject) => {  // ? return a promise, so Cloud Functions knows to wait
        var MongoClient = require('mongodb').MongoClient;
        var url = "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false";
        MongoClient.connect(url, function(err, db) {
            if (err) throw err;
            var dbo = db.db("discord_pandemic");
            dbo.collection("ArrayStats").find().toArray(function(err, result) {
                if (err) throw err;
                const totalInfections = result[0].total_infections
                const totalUsers = result[0].total_users
                const totalCommands = result[0].total_commands
                const totalLiveInfections = result[0].total_live_infections
                const totalLiveInfectedChannels = result[0].total_live_infected_channels
                const data = {
                    totalInfections: totalInfections,
                    totalUsers: totalUsers,
                    totalCommands: totalCommands,
                    totalLiveInfections: totalLiveInfections,
                    totalLiveInfectedChannels: totalLiveInfectedChannels
                }
                console.log(data)
                resolve(data); // ? resolve the promise with the data for the user
            });
        });
      });
    });
    

    由于您不熟悉这种异步行为,我强烈建议您查看:

    【讨论】:

    • 感谢您解释我在这里做错了什么。我现在注意到这是一个问题。我已经按照您的建议进行了更改。但是,最近我对它不起作用的另一个原因有了一个新的想法。新的解决方案仍然返回{data: null},我认为这是由于没有调用该函数?我在 fullName 函数中放置了一个 console.log() ,当我单击按钮时,函数日志中没有 console.log 。但是,响应仍然会登录到浏览器控制台。
    • 如果函数返回一个值,似乎它正在被调用,或者我错过了什么?无论如何:这里可能会出现更多问题,因为您正在集成三项复杂的技术。我所知道的是,我上面解释的肯定是。您发布的代码存在问题。
    • 你是对的。事实证明,这是因为我在本地测试我的功能时没有// firebase.functions().useFunctionsEmulator("http://localhost:5001"); // test functions locally。非常感谢您的帮助!
    猜你喜欢
    • 2020-08-15
    • 2020-06-03
    • 1970-01-01
    • 2021-12-16
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2018-11-24
    相关资源
    最近更新 更多