簡體   English   中英

從本地機器訪問 firebase 數據庫

[英]access to firebase database from local machine

我是 javascript、web 開發和 firebase 的新手。 我正在嘗試使用簡單的腳本來學習它。

我寫了以下腳本:

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");

我得到以下信息:

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

我嘗試了不同的選項,但我一直收到此權限被拒絕錯誤。 我該如何解決?

編輯 1:

在我的 Firebase 儀表板中。 Database - Rules部分我有:

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

據我了解,這應該意味着每個人都可以訪問數據庫。

我認為您應該配置數據庫規則才能使用它。 查看以下鏈接以了解更多信息。

編輯:

檢查和添加規則的步驟:

  1. 登錄https://console.firebase.google.com/並選擇您的數據庫。 在側面板上,您應該單擊database ,在您的主面板上,將會有兩張卡片。 一個是 Cloud Firestone,第二個是實時數據庫。
  2. 選擇實時數據庫。 當數據庫打開時,它的頂部有一個新菜單,上面寫着“數據” | 《規則》 | “備份” | “用法”。
  3. 點擊Rules
  4. 現在設置您自己的規則,以便能夠從數據庫讀取和寫入。 它接受 JSON 格式,如:
{
  "rules": {
    ".read": true,
    ".write": true
  }
}

如果您想了解有關此選項的更多信息,請查看我發布的鏈接。 我希望它能解決你的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM