【问题标题】:Error: admin is not defined using HTTP request with Cloud Functions for Firebase错误:管理员未使用 Cloud Functions for Firebase 的 HTTP 请求定义
【发布时间】:2017-09-14 20:42:21
【问题描述】:

我刚刚为 Firebase 配置了我的第一个 Cloud Functions,在部署了 index.js 文件并将所有更改上传到我的服务器之后。我正在尝试使用我的一个函数,它是一个简单的 HTTP 请求,在我的 Firebase 数据库中的表上创建一个字段:

const functions = require('firebase-functions');

exports.addMessage = functions.https.onRequest((req, res) => {
        // Grab the text parameter.
        const original = req.query.text;
        // Push it into the Realtime Database then send a response
        admin.database().ref('/messages').push({ original: original }).then(snapshot => {
            // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
            res.redirect(303, snapshot.ref);
        });
    });

但是当我在浏览器上调用我的请求时,我在 Functions 控制台上收到一条错误消息,上面写着 admin is not defined:

什么可能导致这个错误?

【问题讨论】:

    标签: javascript firebase firebase-cloud-messaging google-cloud-functions


    【解决方案1】:

    您需要像导入 Cloud Functions SDK 一样导入 Admin SDK 模块:

    const functions = require('firebase-functions');
    
    const admin = require('firebase-admin');
    admin.initializeApp();
    

    可以在here 找到示例代码。这也包含在"get started" guide 中。

    【讨论】:

    • 你是对的!我完全失明,我正在做入门指南示例,但我没有看到那部分。谢谢队友!
    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2019-06-09
    • 2019-05-16
    • 2018-08-13
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多