【发布时间】:2016-04-09 15:19:55
【问题描述】:
我正在尝试使用 Firebase 和 Nodejs 编写 Hello World 类的基本应用程序。
应用程序代码
#cat app.js
var express = require('express');
var Firebase = require('firebase');
var app = express();
app.get('/',function(req,res) {
f();
res.send("success");
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
var onComplete = function(error) {
if (error) {
console.log('Synchronization failed');
} else {
console.log('Synchronization succeeded');
}
};
function f() {
var ref = new Firebase("https://dazzling-torch-XXXXX.firebaseio.com");
ref.set({
location: {
city: "San Francisco",
state: "California",
zip: 94103
}
},onComplete);
ref.child("location/zip").on("value", function(snapshot) {
console.log(snapshot.val()); // Alerts "San Francisco"
});
}
库版本 -
npm --version && node --version && firebase --version
1.3.6
v0.10.42
2.2.1
节点命令-
- node app.js
- 打开浏览器http://localhost:3000/
调试器输出
p:0: Making a connection attempt
c:0:17: Connection created
c:0:17:0 Websocket connecting to wss://dazzling-torch-XXXX.firebaseio.com/.ws?v=5
c:0:17:0 WebSocket error. Closing connection.
c:0:17:0 Network error: wss://dazzling-torch-XXXX.firebaseio.com/.ws?v=5: getaddrinfo ESRCH
c:0:17:0 WebSocket is closing itself
c:0:17:0 Websocket connection was disconnected.
c:0:17: Realtime connection failed.
c:0:17: Closing realtime connection.
c:0:17: Shutting down all connections
p:0: data client disconnected
p:0: Trying to reconnect in 17797.816018858393ms
但它不会更新 firebase 数据。我正在使用默认的安全规则。
谢谢 帕里
【问题讨论】:
-
任何错误信息?当你向/路由发出请求时,你会得到什么?
-
ref.set()采用可选的第二个回调函数,在出现任何错误时将调用该函数(参见the docs for info)。设置它并记录错误可能会提示正在发生的事情。 -
感谢您的回复。我添加了调试器。调试输出 - c:0:2: Connection created c:0:2:0 Websocket connected to wss://dazzling-torch-1380.firebaseio.com/.ws?v=5 c:0:2:0 WebSocket error .关闭连接。 c:0:2:0 网络错误:wss://dazzling-torch-1380.firebaseio.com/.ws?v=5: getaddrinfo ESRCH c:0:2:0 WebSocket 正在关闭自己 c:0:2: 0 Websocket 连接已断开。 c:0:2:实时连接失败。 c:0:2:关闭实时连接。 c:0:2: 关闭所有连接 p:0: 数据客户端断开
-
在主帖中为设置和调试器输出添加了 onComplete 回调函数。谢谢
-
不是一个真正的答案,但我的步骤的输出如下。