gpt4 book ai didi

javascript - 从本地机器访问 firebase 数据库

转载 作者:行者123 更新时间:2023-11-30 19:29:03 24 4
gpt4 key购买 nike

我是 javascript、网络开发和 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;
}
}
}

据我所知,这应该意味着每个人都可以访问该数据库。

最佳答案

我认为您应该配置数据库规则才能使用它。检查以下 link了解更多信息。

编辑:

检查和添加规则的步骤:

  1. 登录https://console.firebase.google.com/并选择您的数据库。在侧面板上,您应该单击 database,然后在您的主面板上,将有两张卡片。一个是 Cloud Firestone,第二个是实时数据库。
  2. 选择实时数据库。打开数据库后,您会在其顶部看到一个新菜单,上面写着“数据” | “规则” | “备份” | “用法”。
  3. 点击规则
  4. 现在设置您自己的规则,以便能够从数据库中读取和写入。它接受 JSON 格式,例如:
{
"rules": {
".read": true,
".write": true
}
}

如果您想找到有关此选项的更多信息,请查看我发布的链接。我希望它能解决您的问题。

关于javascript - 从本地机器访问 firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56621315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com