我正在尝试使用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
我可能做错了什么。有人可以帮忙。
答案 0 :(得分:1)
您似乎还没有创建服务帐户,以便使用节点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"
});
答案 1 :(得分:1)
您需要更改
var firebase = require('firebase/app');
到
var firebase = require('firebase');
答案 2 :(得分:0)
解决方案:将以下内容添加到Webpack配置的根目录中:
resolve: {
mainFields: ['main']
}
<强>解释强>
firebase/database
个包defines different entry points:main
,browser
和module
。 NodeJS使用main
。 Webpack是一个时髦人士,使用module
。
Firebase人员在main
定义的入口点中进行了一些特定于NodeJS的配置(例如将 WebSocket设置为传输层)。因此,在捆绑之后,特定的NodeJS特定代码不会被设置,因此它会出错。告诉Webpack只使用类似Node的main
来解决这个问题。