无法获取节点JS应用程序中的Firebase运行

我正在尝试使用NodeJS设置firebase JS客户端。 到目前为止,这是我的代码

var firebase = require('firebase/app'); require('firebase/database'); var config = { apiKey: "MY_SECRET_KEY_fhcWICPI", authDomain: "my_fir_app.firebaseapp.com", databaseURL: "https://my_fir_app.firebaseio.com", }; var firApp = firebase.initializeApp(config); firebase.database.enableLogging(true) // Get a reference to the database service var database = firebase.database(); 

那么这里是我的Firebasefunction之一,用于将数据保存到实时数据库。

 /** * This will save the authors of stories in Firebase * @param {String} id The ID of the author * @param {String} firstName The authors first name * @param {String} lastName The authors last name * @param {Timestamp} dateCreated The unix time stamp when the author was created */ function saveStoryAuthor(id, firstName, lastName, dateCreated) { database.ref('mystoriesdb/authors/' + id).set({ first_name: firstName, last_name: lastName, date_created : dateCreated }); } 

最后,我的代码中间的某个地方正在调用这个函数

 ... saveStoryAuthor('MZ8XWXNrkG', 'Dennis', 'Richie') ... 

不过,这是我在日志中得到的(因为我已经启用日志logging)

 $ node index.js p:0: Browser went online. p:0: Making a connection attempt getToken() completed. Creating connection. c:0:0: Connection created p:0: Failed to get token: Error: No transports available p:0: data client disconnected p:0: Trying to reconnect in 326.9669258513522ms 0: onDisconnectEvents p:0: Making a connection attempt getToken() completed. Creating connection. c:0:1: Connection created p:0: Failed to get token: Error: No transports available 

我可能做错了什么。 有人可以帮忙吗?

看来你还没有创build一个服务帐户,以添加firebase到您的项目与节点js

查看这里的文档。

 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" }); 

你需要改变

 var firebase = require('firebase/app'); 

 var firebase = require('firebase');