2

I have installed and run firebase admin on my node server, it has been running well until today, there is no given error to tell what happened, it simply stops working.

var admin = require("firebase-admin");

            var serviceAccount = require("mycom-firebase-adminsdk-d1ebt123456.json");

            var app = FireBaseAdapter.admin.initializeApp({
                credential: FireBaseAdapter.admin.credential.cert(serviceAccount),
                databaseURL: "https://xxxx.firebaseio.com"
            });

var db = app.database();
var ref = db.ref("messages"); // this node actually exists in my db.


ref.on("value", function(snapshot) {
// this will never be called - which has been working before.

  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

I even wrap it in the try catch to print out the error, but there isn't any. I can log into the firebase console in the account and see my database with no change (small database) (although it seems be slower than normal). Is there something wrong with firebase Admin SDK? Any help is appreciated.

4

1 回答 1

4

花了几个小时找出原因后,我发现了问题所在。由于我找不到任何方法来启用 firebase-admin 的日志,所以在一切静默运行的情况下解决问题是死路一条,所以我改用firebase包进行日志记录

var firebase = require("firebase");
firebase.initializeApp({
    databaseURL: "https://xxxxx.firebaseio.com",
    serviceAccount: '....'
});

firebase.database.enableLogging(true); // <=== important

我的问题与这个问题非常相似

然后我可以看到实际的错误:

p:0:获取令牌失败:错误:刷新访问令牌时出错:invalid_grant(无效的 JWT:令牌必须是短期令牌并且在合理的时间范围内)

此问题的解决方案已在此 anwser上进行了解释。此问题是由于执行代码的计算机时钟同步不佳导致延迟 5 分钟(由于内部时钟的电池故障)。当我手动将计算机的内部时间更改为正确的时间(或完全重置计算机的日期时间)时,它再次开始工作。

就我而言,重置日期时间和时区后,firebase 会自动再次工作,我不需要重新生成另一个服务帐户。

于 2016-12-11T15:33:58.353 回答