大家下午好,我在尝试将我的快速服务器连接到我的firebase数据库时遇到了一些麻烦。我正在创建一个应用程序,使api调用我的快速服务器,然后从我的客户端发送的信息发出api调用this halo api.从那里从halo的服务器返回给我的信息被发送回我的快速服务器它将一个对象返回给我的客户端来填充DOM。
上面描述的所有内容我已经开始工作了,但是现在我想要合并一个firebase数据库并且事情并不是那么完美。我希望能够做的是在我的应用程序中添加更多功能。因此,当我的快速服务器(app.js
)向光环服务器发出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
根据这个错误,我认为我不需要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"
}
}
我感谢任何人花时间看这个问题并提供他们自己的两分钱。非常感谢,让我知道如何通过提供其他任何东西来帮助你。
答案 0 :(得分: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)
});
});
此代码将数据写入我的数据库!