Firebase数据库推送正在生成密钥,但未显示更改

时间:2018-03-20 07:57:05

标签: node.js angular firebase firebase-realtime-database

我正在尝试创建Node Js服务器,在调用特定的rest api时,它将更改firebase中的一些数据。

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var admin = require("firebase");

admin.initializeApp({
  serviceAccount: './<some-name>nsdk-dgimw-1cab1079fa.json',
  databaseURL: "https://<some-name>.firebaseio.com"
});

// define a simple route
app.get('/', function(req, res){
    res.json({"message": "Welcome to EasyNotes application. Take notes quickly. Organize and keep track of all your notes."});
});

app.get('/getMe', function(req, res){
    var ref = admin.database().ref();
    var childBlog = ref.child('/bloggers');
    // var userRef = childBlog.push();


    var userRef = childBlog.push({
        name: 'Christopher',
        description: 'I eat too much ice cream'
    });
    console.log('user key', userRef.key);

    var myInt = setTimeout(function () {
        console.log("Hello");
    }, 10000);
});

// listen for requests
app.listen(3000, function(){
    console.log("Server is listening on port 3000");
});

这段特殊的代码正在创建一个服务器,其余的api调用工作正常。但是当我插入firebase推送代码时,它会记录生成的推送密钥,但是当我访问firebase数据库时,我发现没有任何更改。

我遵循了article

我想知道我是否做了正确的事情,如果没有,那将是正确的解决方案?

数据库结构 -

-tech-bloggie
  -articles
  -bloggers
  -comments



Warning :
@firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"getaddrinfo ENOTFOUND accounts.google.com accounts.google.com:443\"."}

1 个答案:

答案 0 :(得分:0)

预期的SDK是firebase-admin

$ npm install firebase-admin --save

所以,在你的情况下,它将是:

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

更新SDK,您的代码按预期工作:

您的更新代码:

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

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

var serviceAccount = require("./config/serviceAccountKey.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://shining-fire-7601.firebaseio.com"
});

// define a simple route
app.get('/', function(req, res){
    res.json({"message": "Welcome to EasyNotes application. Take notes quickly. Organize and keep track of all your notes."});
});

app.get('/getMe', function(req, res){
    var ref = admin.database().ref();
    var childBlog = ref.child('/bloggers');
    // var userRef = childBlog.push();


    var userRef = childBlog.push({
        name: 'Christopher',
        description: 'I eat too much ice cream'
    });
    console.log('user key', userRef.key);

    var myInt = setTimeout(function () {
        console.log("Hello");
    }, 10000);
});

// listen for requests
app.listen(3000, function(){
    console.log("Server is listening on port 3000");
});

预期的控制台日志输出:

# node stack
Server is listening on port 9084
user key -L82E-smqbJ7CDMBXK4K
Hello
user key -L82ETNDTdGa6jZqbFQ1
Hello
user key -L82Ex-SWQq_hPV1YRBt
Hello

firebase控制台:

enter image description here

请注意您的警告:必须正确配置SDK:https://firebase.google.com/docs/admin/setup?hl=en-419

基本上,你需要这个:

admin.initializeApp({
  credential: admin.credential.cert({
    projectId: '<PROJECT_ID>',
    clientEmail: 'foo@<PROJECT_ID>.iam.gserviceaccount.com',
    privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n'
  }),
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

这是你需要做的:

&#34; 初始化SDK - 创建Firebase控制台项目并使用您的服务帐户凭据下载JSON文件后,您可以使用此代码段初始化SDK ... & #34;