简体   繁体   English

错误:管理员未使用带有 Cloud Functions for Firebase 的 HTTP 请求定义

[英]Error: admin is not defined using HTTP request with Cloud Functions for Firebase

I just configured my first Cloud Functions for Firebase and after deploying the index.js file and upload all the changes to my server.我刚刚为 Firebase 配置了我的第一个 Cloud Functions,在部署index.js文件并将所有更改上传到我的服务器之后。 I'm trying to use one of my functions which is a simple HTTP request to create a field on a table in my Firebase Database:我正在尝试使用我的一个函数,它是一个简单的 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);
        });
    });

But when I'm calling to my request on my browser I'm receiving an error on my Functions console which says admin is not defined :但是,当我在浏览器上调用我的请求时,我在我的 Functions 控制台上收到一条错误消息,提示admin is not defined

在此处输入图片说明

What might be causing this error?什么可能导致此错误?

You need to import the Admin SDK module just like you are importing the Cloud Functions SDK:您需要像导入 Cloud Functions SDK 一样导入 Admin SDK 模块:

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

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

Example code can be found here .示例代码可以在这里找到。 This is also covered in the "get started" guide .这也包含在“入门”指南中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM