我想使用firebase作为我的服务器的数据库服务,android应用程序将向其发送请求。我希望服务器检索数据而不是Android应用程序,因为我想在将数据发送回客户端(app)之前进行一些处理。
我的节点代码(index.js):
const express = require('express')
const app = express()
var port = process.env.PORT || 10000
firebase = require('./firebase') // I added this cuz I can't use <script> tag here
// Initialize Firebase
var config = {
apiKey: "AIzaSynotgonnatell2Q-kwk85XrCyNnc",
authDomain: "hnotgonnatell.firebaseapp.com",
databaseURL: "https://hnotgonnatell.firebaseio.com",
projectId: "notgonnatell",
storageBucket: "",
messagingSenderId: "699935506077"
};
firebase.initializeApp(config);
var database = firebase.database()
ref = database.ref("some_table")
data = {
name : "my name",
number : "2938019283"
}
app.get('/', function(req, res){
ref.push(data)
res.send("hello")
})
app.listen(port)
我无法使用<script src="https://www.gstatic.com/firebasejs/4.12.1/firebase.js"></script>
因为我没有从客户端连接到firebase。所以我改为从https://www.gstatic.com/firebasejs/4.12.1/firebase.js复制代码并使用require()
将其导入我的index.js。当我运行index.js时,我得到:
firebase.initializeApp(config);
^
TypeError: firebase.initializeApp is not a function
at Object.<anonymous> (E:\workspace_javascript\firebase_try\index.js:24:10)
at Module._compile (module.js:571:32)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:422:7)
at startup (bootstrap_node.js:143:9)
at bootstrap_node.js:537:3
通过在<script>
答案 0 :(得分:3)
从NPM安装firebase
npm install --save firebase-admin
初始化firebase
var admin = require('firebase-admin');
var serviceAccount = require('path/to/serviceAccountKey.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});
???
答案 1 :(得分:1)
如果您要在服务器上访问Firebase产品,则应使用Firebase Admin SDK,而不是客户端SDK。