我正在尝试使用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();
然后,这是我的Firebase功能之一,用于将数据保存到实时数据库.
/**
* 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')
...
但是,这是我在日志中获得的(因为我已启用日志记录)
$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
我可能做错了什么.有人可以帮忙吗
最佳答案 您似乎没有创建服务帐户,以便使用节点js将firebase添加到项目中
查看文档here.
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"
});