Search code examples
javascriptnode.jsfirebasewebserver

Writing to Firebase database with node.js web server - "FIREBASE WARNING: set at ... failed: permission_denied"


I have a node.js web server, through which I am trying to write to Firebase. However, whenever it runs I get the following error message:

FIREBASE WARNING: set at /mydb failed: permission_denied 

Here is how I set up my application (relevant details):

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');
  });;
}

Calling the function:

//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');    
};

What do I need to do in order to get Firebase to accept my post? I have been reading over some docs on this:

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


Solution

  • Have you finish your firebase sdk setting? You need to download a JSON file, and put it into your node server. Then initialize firebase with this code.

    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');
      });;
    }
    

    Calling the function:

    //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');    
    };
    

    You can check on this google site

    Add the Firebase Admin SDK to your Server