【问题标题】:Simple firebase cloud function from tutorial docs "could not handle request"教程文档中的简单 Firebase 云功能“无法处理请求”
【发布时间】:2019-01-31 13:01:39
【问题描述】:

按照官方 firebase 云函数教程 (https://firebase.google.com/docs/functions/get-started),尝试实际部署和使用函数时遇到错误 (https://firebase.google.com/docs/functions/get-started#deploy-and-execute-addmessage)。成功完成示例函数的部署并指示“在addMessage() URL中添加文本查询参数,并在浏览器中打开”,结果是文本

错误:无法处理请求

出现在浏览器中。在浏览器中检查开发者控制台,我看到了

加载资源失败:服务器响应状态为500()

(实际上不知道如何解释这一点)并查看 firebase 仪表板中的使用情况统计信息,可以看到该功能正在被激活(只是工作不顺利)。用于部署的确切代码如下所示

//import * as functions from 'firebase-functions';

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
// export const helloWorld = functions.https.onRequest((request, response) => {
//  response.send("Hello from Firebase!");
// });

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();


// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest((req, res) => {
    // Grab the text parameter.
    const original = req.query.text;
    // Push the new message into the Realtime Database using the Firebase Admin SDK.
    return admin.firestore().ref('/messages').push({original: original})
    .then((snapshot) => {
        // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
        return res.redirect(303, snapshot.ref.toString());
    });
});

以前从未使用过谷歌云功能,如果有任何调试信息或此处可能出错的解决方案,我们将不胜感激。


最终,希望最终使用云函数与 firestoreDB 一起使用,而官方教程似乎旨在与 firebaseDB 一起使用。因此,如果在这方面需要进行任何特定的更改,我们也将不胜感激。

【问题讨论】:

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


    【解决方案1】:

    您的代码中的以下行是错误的:

    return admin.firestore().ref('/messages').push({original: original}).then()
    

    您正在“混淆”应该以不同方式查询(即写入、读取、删除)的实时数据库和 Firestore,因为它们的数据模型不同,请参阅https://firebase.google.com/docs/firestore/rtdb-vs-firestore。特别是,虽然“实时数据库和 Cloud Firestore 都是 NoSQL 数据库”,但第一个“将数据存储为一棵大型 JSON 树”,而后者“将数据存储在按集合组织的文档中”。

    事实上,您所指的“入门”帮助项中的原始代码是针对实时数据库的admin.database()

    return admin.database().ref('/messages').push({original: original}).then()
    

    在这行代码中,将database() 替换为firestore() 将不起作用。

    您应该查看 Firestore 文档,例如 hereherehere,以了解如何写入 Firestore。

    例如,您可以如下修改云函数:

    exports.addMessage = functions.https.onRequest((req, res) => {
        // Grab the text parameter.
        const original = req.query.text;
    
        admin.firestore().collection("mydocs").doc("firstdoc").set({original: original})
        .then(() => {
           console.log("Document successfully written!");
           res.send({original: original});  //Just an example, as there is not that much interest to send back the value of original...  
        })
        .catch(error => {
           console.error("Error writing document: ", error);
           res.status(500).send(error);
       });
    })
    

    最后,我建议您观看以下官方视频系列“Learning Cloud Functions for Firebase”(here),尤其是名为“Learn JavaScript Promises”的三个视频。您会特别注意到,对于 HTTP 触发函数您应该只发送响应,而不使用 return

    【讨论】:

    • 我明白了,问题是(正如您所指出的)在试验时将 admin.database() 行更改为 admin.firestore() 并且在部署功能时没有注意到这一点。无论如何,感谢您提供额外的资源和转换后的示例。
    • 很高兴我能帮上忙!还要注意return 上的点,这很可能是导致错误的原因。视频系列中对此进行了很好的解释。如果您认为它“有用且经过充分研究”,您也可以投票赞成我的回答,请参阅stackoverflow.com/help/someone-answers。谢谢。
    猜你喜欢
    • 2018-01-17
    • 2018-07-03
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2019-06-13
    • 2019-06-09
    相关资源
    最近更新 更多