使用node.js Web服务器写入Firebase数据库 - “FIREBASE WARNING:设置为... failed:permission_denied”

时间:2017-04-27 21:42:30

标签: javascript node.js firebase webserver

我有一个node.js网络服务器,我试图写入Firebase。但是,无论何时运行,我都会收到以下错误消息:

FIREBASE WARNING: set at /mydb failed: permission_denied 

以下是我设置应用程序的方法(相关详情):

var firebase = require('firebase');

var config = {
  apiKey: "8oc57xxx",
  authDomain: "example-04.firebaseapp.com",
  databaseURL: "https://example-ex04.firebaseio.com",
  projectId: "example-ex04",
  storageBucket: "example-ex04.appspot.com",
  messagingSenderId: "5022"
};

firebase.initializeApp(config);

function writeData(details) {
  firebase.database().ref('mydb/'). push({
    title: details.title,
    description: details.description,
    tags: details.tags
  }).then(function() {
    console.log('Upload succeeded');
  }).catch(function(error) {
    console.log('Upload failed');
  });;
}

调用该函数:

//what details looks like:
//{ title: 'Details',
  //description: 'Really fast, and awesome.',
  //tags: 'tag-1' 
//}

exports.postDetails = (req, res, next) => {
  writeData(req.body);

  const errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    return res.redirect('/example');
  } 

  req.flash('success', { msg: 'Success!' });    
  res.redirect('/example');    
};

为了让Firebase接受我的帖子,我需要做些什么?我一直在阅读一些关于此的文档:

https://firebase.google.com/docs/reference/js/firebase.database.Reference#set

1 个答案:

答案 0 :(得分:1)

你完成了firebase sdk设置吗? 您需要下载JSON文件,并将其放入节点服务器。 然后使用此代码初始化firebase。

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

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  apiKey: "8oc57xxx",
  authDomain: "example-04.firebaseapp.com",
  databaseURL: "https://example-ex04.firebaseio.com",
  projectId: "example-ex04",
  storageBucket: "example-ex04.appspot.com",
  messagingSenderId: "5022"
});

function writeData(details) {
  admin.database().ref('mydb/').push({
    title: details.title,
    description: details.description,
    tags: details.tags
  }).then(function() {
    console.log('Upload succeeded');
  }).catch(function(error) {
    console.log('Upload failed');
  });;
}

调用该函数:

//what details looks like:
//{ title: 'Details',
  //description: 'Really fast, and awesome.',
  //tags: 'tag-1' 
//}

exports.postDetails = (req, res, next) => {
  writeData(req.body);

  const errors = req.validationErrors();

  if (errors) {
    req.flash('errors', errors);
    return res.redirect('/example');
  } 

  req.flash('success', { msg: 'Success!' });    
  res.redirect('/example');    
};

您可以查看此google网站

enter image description here