最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在firebase函数中使用类吗?

网站源码admin26浏览0评论

在firebase函数中使用类吗?

在firebase函数中使用类吗?

我正在为firebase函数编写代码,问题是我需要使用一个类,但是当我调用class方法时,firebase函数日志显示此错误:

错误:

ReferenceError: Proverbi is not defined
at exports.getProverbio.functions.https.onRequest (/srv/index.js:48:26)
at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:49:16)
at /worker/worker.js:783:7
at /worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:132:7)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)

这里是“ index.js”代码:

//firebase deploy --only functions

// 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.addStanza = functions.https.onRequest(async (req, res) => {
    // Grab the text parameter.
    const nome = req.query.text;
    // Push the new message into the Realtime Database using the Firebase Admin SDK.
    const snapshot = await admin.database().ref('/stanze').push({giocatori: {giocatore:{nome:nome,punteggio:0}}});
    // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
    //res.redirect(200, nome.toString());
    var link = snapshot.toString().split('/');

    res.json({idStanza:link[4]});
});

// Listens for new messages added to /messages/:pushId/original and creates an
//  uppercase version of the message to /messages/:pushId/uppercase

 exports.addFirstPlayer = functions.database.ref('/stanze/{pushId}/giocatori/giocatore/nome')
.onCreate((snapshot, context) => {
  // Grab the current value of what was written to the Realtime Database.
  const nome = snapshot.val();
 // const snapshot3 = snapshot.ref('/stanza/{pushId}/giocatori/giocatore').remove();
  const snapshot2 =  snapshot.ref.parent.parent.remove();
  return snapshot.ref.parent.parent.push({nome:nome,punteggio:0});
});

exports.addPlayer = functions.https.onRequest(async (req, res) => {
  // Grab the text parameter.
  const nome = req.query.text;
  const idStanza = req.query.id;
  // Push the new message into the Realtime Database using the Firebase Admin SDK.
  const snapshot = await admin.database().ref('/stanz/'+idStanza+"/giocatori").push({nome:nome,punteggio:0 });
  // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
  //res.redirect(200, nome.toString());
  res.json({success:{id:idStanza}});
});

exports.getProverbio = functions.https.onRequest(async (req, res) => {
    const difficolta = req.query.difficolta;
    var proverbioClass = new Proverbi(2);
    var p = proverbioClass.getProverbio();
    var proverbio = p.split('-');
    var inizio = proverbio[0];
    var fine = proverbio[1];

    res.json({proverbio:{inizio:inizio,fine:fine,difficolta:difficolta}});
});

这是引起问题的代码:

exports.getProverbio = functions.https.onRequest(async (req, res) => {
    const difficolta = req.query.difficolta;
    var proverbioClass = new Proverbi(2);
    var p = proverbioClass.getProverbio();
    var proverbio = p.split('-');
    var inizio = proverbio[0];
    var fine = proverbio[1];

    res.json({proverbio:{inizio:inizio,fine:fine,difficolta:difficolta}});
});

这里是“ Proverbi.class”代码:

class Proverbi{
    constructor(n) {
        this.magicNumber = n;
    }
    getProverbio() {
        var text = "";
        switch (magicNumber) {
            case 1:
                text += ("Chip");
                text += ("-Chop");
                break; 
        }
        return text;
    }
}

如何在“ index.js”中使用“ Proverbi”类?

回答如下:您需要将Proverbi类的定义添加到index.js文件中。

如果您仅打算在getProverbio Cloud Function中使用该类,请执行以下操作:

exports.getProverbio = functions.https.onRequest(async (req, res) => { class Proverbi { constructor(n) { this.magicNumber = n; } getProverbio() { var text = ""; switch (this.magicNumber) { case 1: text += ("Chip"); text += ("-Chop"); break; } return text; } } const difficolta = req.query.difficolta; var proverbioClass = new Proverbi(2); var p = proverbioClass.getProverbio(); var proverbio = p.split('-'); var inizio = proverbio[0]; var fine = proverbio[1]; res.json({ proverbio: { inizio: inizio, fine: fine, difficolta: difficolta } }); });

如果要在其他函数中使用Class,则只需如下声明:

class Proverbi { constructor(n) { console.log(n); } getProverbio() { console.log(this.magicNumber); console.log(this.magicNumber); var text = ""; switch (this.magicNumber) { case 1: text += ("Chip"); text += ("-Chop"); break; } return text; } } exports.getProverbio = functions.https.onRequest(async (req, res) => { const difficolta = req.query.difficolta; var proverbioClass = new Proverbi(2); var p = proverbioClass.getProverbio(); var proverbio = p.split('-'); var inizio = proverbio[0]; var fine = proverbio[1]; res.json({ proverbio: { inizio: inizio, fine: fine, difficolta: difficolta } }); });

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论