简体   繁体   中英

access to firebase database from local machine

I am new to javascript, web development and firebase. I am trying to learn it using simple scripts.

I wrote the following script:

console.log("Starting")
var firebase = require("firebase/app");

// Add the Firebase products that you want to use
require("firebase/auth");
require("firebase/firestore");
require('firebase/database');


console.log("Firebase was imported")

var firebaseConfig = {
    apiKey: ...,
    authDomain: ...,
    databaseURL: ...,
    projectId: ...,
    storageBucket: ...,
    messagingSenderId: ...,
    appId: ...
  };
  // Initialize Firebase

console.log("Firebase  initializition")
firebase.initializeApp(firebaseConfig);

console.log("connect to db")
var database = firebase.database();


function writeApartmentData(apartmentId, street_, number_, floor_, door_) {
    firebase.database().ref('apartments/' + apartmentId).set({
      street: street_,
      number: number_,
      floor: floor_,
      door: door_

    });
  }

console.log("write apartment data")
writeApartmentData(1, "calle amalia", 18, 3, "B")



console.log("End");

and I get the following:

luca@luca-VirtualBox:~/PisoReview$ node index.js 
Starting
Firebase was imported
Firebase  initializition
connect to db
write apartment data
End
[2019-06-16T17:52:19.193Z]  @firebase/database: FIREBASE WARNING: set at /apartments/1 failed: permission_denied 
(node:10870) UnhandledPromiseRejectionWarning: Error: PERMISSION_DENIED: Permission denied
    at /home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:13139:33
    at exceptionGuard (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:695:9)
    at Repo.callOnCompleteCallback (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:13130:13)
    at /home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:12907:19
    at /home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:12078:17
    at PersistentConnection.onDataMessage_ (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:12111:17)
    at Connection.onDataMessage_ (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:11394:14)
    at Connection.onPrimaryMessageReceived_ (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:11388:18)
    at WebSocketConnection.onMessage (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:11289:27)
    at WebSocketConnection.appendFrame_ (/home/luca/PisoReview/node_modules/@firebase/database/dist/index.node.cjs.js:10892:18)
(node:10870) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:10870) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
^C
luca@luca-VirtualBox:~/PisoReview$ ^C

I have tried different options but I kept getting this Permission denied error. How can I fix it ?

EDIT 1:

In my firebase dashboard. In the Database - Rules section I have:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if true;
    }
  }
}

As far as I understood this should mean that the database is accessible by everybody.

I think that you should configure database rules in order to use it. Check the following link to find out more about it.

EDIT:

Steps to check and add rules:

  1. Login to https://console.firebase.google.com/ and choose your database. On the sidepanel, you should click database and on your main panel, there will be two cards. One is Cloud Firestone and the second one is Realtime Database.
  2. Choose Realtime Database. When database is opened, you have new menu on the top of it that says "Data" | "Rules" | "Backups" | "Usage".
  3. Click on Rules .
  4. Now setup your own rules to be able to read and write from database. It accepts JSON format like:
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

If you want to find more about this options, please check link that i've posted. I hope it solves your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM