【问题标题】:Using classes in firebase functions?在firebase函数中使用类?
【发布时间】:2020-05-05 11:46:40
【问题描述】:

我正在为 firebase 函数编写代码,问题是我需要使用一个类,但是当我调用类方法时,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”类?

【问题讨论】:

  • 请发布导致问题的代码...
  • 我刚刚添加了代码,希望您现在可以帮助我。
  • 您的代码通常不应该产生这样的错误。您能否添加您的index.js 文件的完整和准确 内容,而不仅仅是一些代码sn-ps。
  • 还要注意你应该做switch (this.magicNumber) {...}
  • 我已经上传了 index.js 代码。

标签: node.js firebase firebase-realtime-database google-cloud-functions


【解决方案1】:

您需要将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 } });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    相关资源
    最近更新 更多