2016-06-24 32 views
1

大家下午好,我在嘗試將我的快遞服務器連接到我的Firebase數據庫時遇到了一些麻煩。我正在製作一個應用程序,它可以讓我的快遞服務器進行api調用,然後我的客戶端發送的信息就可以進行api調用this halo api.從那裏,我們從暈服務器返回給我的信息將返回給我的快遞服務器,回到我的客戶端來填充DOM。將Firebase數據庫連接到Express.js服務器時出現問題

上面所述的一切我已經開始工作了,但是現在我想要合併一個Firebase數據庫,而且事情並不是那麼完美。我想要做的是爲我的應用程序添加更多功能。因此,當我的快遞服務器(app.js)向halo服務器發送api電話時,我想將數據發送到我的firebase數據庫,而不是發回給客戶端。對我來說,在實踐中,取而代之的是我從暈圈接收的數據對象,我現在只是稱它爲光環,因爲它更簡單,並將它發送到我的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 

基於這個錯誤,我想,我並不需要提供火力點,但我正確的文件只是不知道要包括哪一個。我安裝節點包火力點,我已經閱讀教程只使用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": "[email protected]", 
    "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" 
    } 
} 

我很欣賞任何花時間看這個問題並提供他們自己的兩分錢的人。非常感謝,讓我知道如何通過提供其他任何東西來幫助您。

+0

您是否爲node.js創建了服務帳戶? –

+0

@AmiHollander是的,我做到了。我傳遞給'Firebase.initializeApp({...})'的所有信息。 – m00saca

+0

根據此鏈接:https://firebase.google.com/support/guides/firebase-web#get_a_database_reference_numbered 你錯過了兩個變量:'apiKey','authDomain' –

回答

1

使用這個代碼,我是能夠連接到火力在後端:

"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": "[email protected]", 
    "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) 
     }); 
}); 

此代碼寫入數據到我的數據庫!

相關問題