【发布时间】: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" 而不是23 或24),但结果是一样的。
我是 Firebase 的新手,所以这让我很困惑。问题的根本原因是什么?
【问题讨论】:
-
您的代码看起来确实不错。你可以只尝试第二个查询来看看会发生什么吗? (禁用其他三个)。您可以更新到最新的 firebase-admin。
-
@Er.Se 我单独测试了每个调用,并且我还将 firebase-admin 更新到版本 9.9.0,问题仍然存在。
标签: node.js firebase firebase-admin