无法连接Firebase数据库到Express.js服务器

大家下午好,我试图将我的快递服务器连接到我的Firebase数据库时遇到一些麻烦。 我正在做一个应用程序,使API调用我的快递服务器,反过来从我的客户端发送的信息使api调用这个晕api。 从那里从晕的服务器给我回来的信息被送回到我的快递服务器,它给我的客户端对象填充DOM。

上面所描述的一切,我已经得到了工作,耶,但现在我想要合并一个火力点数据库,事情并不完美。 我想要能够做的是添加更多的function与我的应用程序。 所以当这个API调用是从我的快递服务器( app.js )到晕的服务器,我想发送数据到我的firebase数据库,而不是回到客户端。 对我来说,在实践中,取而代之的是我从halo那里得到的数据对象,我现在只是把它称为晕,因为它更容易,并将其发送到我的firebase数据库。 从那里我将使用数据库来填充DOM。

正如我前面提到的,在实践中这certificate要困难得多。 我需要首先使用firebase,因为我需要添加CRUDfunction到这个应用程序,所以只是不使用它是不可能的。 目前运行我的服务器时,我运行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文件只是incase:

 { "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" } } 

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

使用这段代码,我可以连接到后端的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) }); }); 

这段代码将数据写入我的数据库!