【问题标题】:Trouble connecting Firebase database to Express.js server将 Firebase 数据库连接到 Express.js 服务器时出现问题
【发布时间】:2016-06-26 02:10:17
【问题描述】:

大家下午好,我在尝试将我的 express 服务器连接到我的 firebase 数据库时遇到了一些麻烦。我正在制作一个应用程序,该应用程序对我的快速服务器进行 api 调用,然后使用从我的客户端发送的信息对this halo api. 进行 api 调用,从那里,从 halo 的服务器返回给我的信息被返回给我的快速服务器,它给出一个对象返回给我的客户来填充 DOM。

上面描述的一切我已经开始工作了,是的,但现在我想合并一个 firebase 数据库,但事情并不是那么完美。我想要做的是在我的应用程序中添加更多功能。因此,当从我的快速服务器 (app.js) 向 halo 的服务器发出 api 调用时,我想将数据发送到我的 firebase 数据库而不是返回到客户端。对我来说,实际上很容易取而代之的是从 halo 接收到的数据对象,我现在只是将其称为 halo,因为它更容易,并将其发送到我的 firebase 数据库。从那里我将使用数据库来填充 DOM。

正如我之前提到的,在实践中,这被证明要困难得多。我首先需要使用 firebase,因为我需要向这个应用程序添加 CRUD 功能,所以不使用它是不可能的。目前在运行我的服务器时,我在运行 nodemon src/app.js 后在命令行中收到此错误:

/Users/MMac/Desktop/haloApp/src/app.js:28
var dbReference =  new Firebase("https://haloapp-5cfe2.firebaseio.com/");
                   ^

TypeError: Firebase is not a function
    at Object.<anonymous> (/Users/MMac/Desktop/haloApp/src/app.js:28:20)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:968:3

基于此错误,我认为我不需要正确的 firebase 文件,但我只是不知道要包含哪个文件。我为firebase安装了节点包,我读过的教程只使用var Firebase = require("firebase");

这是我的 app.js 文件:

"use-strict"

var express = require("express");
//dependencies
var request = require("request");
var Options = require("./router.js")
var app = express();
var bodyParser = require("body-parser");
var Firebase = require("firebase");

Firebase.initializeApp({
  databaseURL: "[databaseURL]",
  serviceAccount: {
  "type": "service_account",
  "project_id": "haloapp-5cfe2",
  "private_key_id": "[some number]",
  "private_key": "[redacted]",
  "client_email": "haloappservice@haloapp-5cfe2.iam.gserviceaccount.com",
  "client_id": "[my client ID]",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/haloappservice%40haloapp-5cfe2.iam.gserviceaccount.com"
}

});

var dbReference =  new Firebase("https://haloapp-5cfe2.firebaseio.com/");

这也是我的 package.json 文件,以防万一:

{
  "name": "haloapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.15.1",
    "express": "^4.13.4",
    "firebase": "^3.0.5",
    "nodemon": "^1.9.2"
  }
}

我感谢任何花时间研究这个问题并提供他们自己的两分钱的人。非常感谢,让我知道如何通过提供其他任何东西来帮助您。

【问题讨论】:

  • 您是否为 node.js 创建了服务帐户?
  • @AmiHollander 是的,我做到了。我传递给Firebase.initializeApp({...})的所有信息。
  • 根据这个链接:firebase.google.com/support/guides/…你错过了两个变量:apiKeyauthDomain
  • 我最近使用了这个教程,它正在工作
  • @AmiHollander 我至少能够从以下命令开始链接到我的数据库:var db = Firebase.database(); INSTEAD of var dbReference = new Firebase("https://haloapp-5cfe2.firebaseio.com/"); 我想我可能一直在尝试使用折旧的方法来引用我的数据库。感谢您回复我。

标签: javascript ajax node.js firebase


【解决方案1】:

使用此代码,我能够连接到后端的 firebase:

"use-strict"

var express = require("express");
//dependencies
var request = require("request");
var Options = require("./router.js")
var app = express();
var bodyParser = require("body-parser");
var Firebase = require("firebase");

Firebase.initializeApp({
  databaseURL: "[databaseURL]",
  serviceAccount: {
  "type": "service_account",
  "project_id": "haloapp-5cfe2",
  "private_key_id": "[some number]",
  "private_key": "[redacted]",
  "client_email": "haloappservice@haloapp-5cfe2.iam.gserviceaccount.com",
  "client_id": "[my client ID]",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/haloappservice%40haloapp-5cfe2.iam.gserviceaccount.com"
}

});

// links to my firebase database
var db = Firebase.database();
//sets the reference to the root of the database, or the server I'm not quite sure.
var ref = db.ref("/");

这是一个将数据发送到数据库的示例 API 调用:

//Event Listener when there is a POST request made from public/request.js
app.post("/statSearch", function(req, res){
    // In this case the req is the POST request and the request body is the data I sent along with it. Refer to request.js
    var search = req.body.search;

    var statsOptions = new Options("https://www.haloapi.com/stats/h5/servicerecords/warzone?players="+search);

        request(statsOptions, function (error, response, body) {
          if (error) throw new Error(error);
          // This is necessary because the body is a string, and JSON.parse turns said string into an object
          var body = JSON.parse(response.body)

          var playerData = {
            gamertag: body.Results[0].Id,
                totalKills: body.Results[0].Result.WarzoneStat.TotalKills,
                totalDeaths: body.Results[0].Result.WarzoneStat.TotalDeaths,
                totalGames: body.Results[0].Result.WarzoneStat.TotalGamesCompleted
          };
        res.send(playerData)
        //console.log(playerData);
    // creates a child named "user" in my database
    var userRef = ref.child("user");
    // populates the child with the playerData object successfully.
    // Every time a new POST request is issued the user's data resets.
    userRef.set(playerData)
        });
});

此代码将数据写入我的数据库!

【讨论】:

    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多