【问题标题】:Firebase Realtime Database data snapshot will have no value when there are more children in the hierarchy当层次结构中有更多子级时,Firebase 实时数据库数据快照将没有价值
【发布时间】:2021-06-16 20:40:19
【问题描述】:

我有一个 node.js 服务器应用程序,它使用 firebase-admin (8.13.0 9.9.0) 来访问 Firebase 的实时数据库(旧版),但存在一个问题。当目标子树中有多个子树时,它无法接收特定快照的值。至少我认为这是问题的症状。

请查看此示例数据:

上面的所有对象都被填充了。

这是代码(server.js):

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var http = require("http");
setting_detail = {};

init();

function init() {
    var pkg = require('./package.json');

    var config = require('./config/config'),
        mongoose = require('./config/mongoose'),
        express = require('./config/express'),
        db = mongoose(),
        app = express();
    const port = '8000';
    app.listen(port);

    var Setting = require('mongoose').model('setting');
    Setting.findOne({}, function (error, setting) {

        setting_detail = setting
        var admin = require("firebase-admin");
        var serviceAccount = {
            "type": setting_detail.type,
            "project_id": setting_detail.project_id,
            "private_key_id": setting_detail.private_key_id,
            "private_key": setting_detail.private_key,
            "client_email": setting_detail.client_email,
            "client_id": setting_detail.client_id,
            "auth_uri": setting_detail.auth_uri,
            "token_uri": setting_detail.token_uri,
            "auth_provider_x509_cert_url": setting_detail.auth_provider_x509_cert_url,
            "client_x509_cert_url": setting_detail.client_x509_cert_url
        };

        admin.initializeApp({
            credential: admin.credential.cert(serviceAccount),
            databaseURL: setting_detail.databaseURL
        });

        admin.database().ref("6017a15dd78fa359be7aa28c").child("24").once("value", function(snapshot) {
                var test = snapshot.val();
                console.log(test); // will print an object
        });

        admin.database().ref("601a72368fd78074daa977c8").child("24").once("value", function(snapshot) {
                var test = snapshot.val();
                console.log(test); // will print null
        });

        admin.database().ref("601a8c9e8fd78074daa977e1").child("23").once("value").then((snapshot) => {
                var test = snapshot.val();
                console.log(test); // will print null, even on promises
        });

        admin.database().ref("601a8c9e8fd78074daa977e1").child("24").once("value", function(snapshot) {
                var test = snapshot.val();
                console.log(test); // will print null
        });

    });
    exports = module.exports = app;
}

serviceAccount 内容:

auth_provider_x509_cert_url:'https://www.googleapis.com/oauth2/v1/certs'
auth_uri:'https://accounts.google.com/o/oauth2/auth'
client_email:'firebase-adminsdk-*****@store-******.iam.gserviceaccount.com'
client_id:'109******************'
client_x509_cert_url:'https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-*****%40store-******.iam.gserviceaccount.com'
private_key:'-----BEGIN PRIVATE KEY-----\n********************************************************************************************\n-----END PRIVATE KEY-----\n'
private_key_id:'f23*************************************'
project_id:'store-******'
token_uri:'https://oauth2.googleapis.com/token'
type:'service_account'

控制台输出:

[nodemon] starting `node server.js`
(node:55737) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
(Use `node --trace-deprecation ...` to show where the warning was created)
(node:55737) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:55737) DeprecationWarning: collection.ensureIndex is deprecated. Use createIndexes instead.
Magic happens on port 8000
{
  '-M_NSc1jcXv3hWDB5R0q': {
    chat_type: 24,
    id: '-M_NSc1jcXv3hWDB5R0q',
    is_notify: false,
    is_read: false,
    message: 'hallo',
    receiver_id: '60178ed135de8657dfb67a8b',
    sender_type: 4,
    time: '2021-05-10T22:47:11.810Z'
  }
}
null
null
null

我也尝试使用字符串而不是数字(即"23""24" 而不是2324),但结果是一样的。

我是 Firebase 的新手,所以这让我很困惑。问题的根本原因是什么?

【问题讨论】:

  • 您的代码看起来确实不错。你可以只尝试第二个查询来看看会发生什么吗? (禁用其他三个)。您可以更新到最新的 firebase-admin。
  • @Er.Se 我单独测试了每个调用,并且我还将 firebase-admin 更新到版本 9.9.0,问题仍然存在。

标签: node.js firebase firebase-admin


【解决方案1】:

我不确定是什么导致了您的问题,因为有很多可能性。 但是,试试这个,看看你得到了什么(你的实时结构不会导致这样的问题,这个问题很可能是阅读方式的问题):

admin.database().ref().child('601a72368fd78074daa977c8')
                .child('24')
                .once('value')
                .then((data) => { 
                   final test = data.val();
                   console.log(test);
                 });
                 

如果仍然无法正常工作,请提供更多代码,并查看服务器或控制台是否出现任何错误或日志。

【讨论】:

  • 不幸的是,使用 Promise 无济于事。我编辑了我的问题以提供包含测试和控制台输出的完整 server.js。凭证是否可能仅对特定儿童具有权限? Firebase 中是否有任何可能导致此类问题的配置?
【解决方案2】:

试试这个

const somefun = async () => {
        const snap1 = await database().ref("6017a15dd78fa359be7aa28c/24").once('value');
        const snap2 = await database().ref("601a72368fd78074daa977c8/24").once('value');
        const snap3 = await database().ref("601a8c9e8fd78074daa977e1/24").once('value');
    
        console.log(snap1.toJSON())
        console.log(snap2.toJSON())
        console.log(snap2.toJSON())
}

使用 Promises 代替回调帮助我解决了 firebase 的一些烦人问题。

希望这有帮助?

【讨论】:

  • 我用 Promise 对其进行了测试,但不幸的是,它没有帮助。
  • 嗨@MartinBraun 如果您使用 Firebase REST API,请检查数据是否可用,请查看此答案以获取参考。 :stackoverflow.com/questions/67979394/…
  • 在密钥601a72368fd78074daa977c8 中有一个孩子24。它的快照将为空,其子项有两个数据条目,-MSbRMAMvfF6ouGHOxRZ-MSbXzGBZjytmEVztUHw。尽管无法使用 admin SDK 加载数据,但我可以通过 JSON 链接毫无问题地访问数据,因此数据可用,但无法从 SDK 加载。
猜你喜欢
  • 2021-03-26
  • 2021-01-21
  • 2021-06-07
  • 2022-01-21
  • 2020-05-11
  • 1970-01-01
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多