Bru*_*ith 1 javascript firebase google-cloud-firestore
我认输了。我似乎无法弄清楚这一点。我已经设置了一个 firebase 应用程序,只是尝试对数据进行简单的请求,但它返回 null。我的数据库目前设置为公开,所以不应该有任何权限问题。我可以使用 npm 包成功进行身份验证,react-firebaseui/StyledFirebaseAuth
并取回用户信息,因此它在某种程度上可以正常工作,但我无法从数据库中获取数据。我已经多次浏览文档并尝试在此处搜索问题,但似乎找不到任何内容。我试图做到彻底。所以这基本上就是我所拥有的......
// The actual config in the code is copied directly from the firebase general settings page, so if it isn't right, it isn't right on their page.
const config = {
apiKey: "someKey",
authDomain: "myAppId.firebaseapp.com",
databaseURL: "https://myAppId.firebaseio.com",
projectId: "myAppId",
storageBucket: "myAppId.appspot.com",
messagingSenderId: "mySenderId"
};
const handleSnapshot = (snapshotVal) => {
console.log(snapshotVal);
// Using react hence the setState, but this and the console.log return null
this.setState({data: snapshotVal});
}
firebase.initializeApp(config);
database = firebase.database()
countriesRef = database.ref('countries');
countriesRef.on('value', function(snapshot) {
handleSnapshot(snapshot.val());
});
Run Code Online (Sandbox Code Playgroud)
我也试过...
countriesRef = database.ref('/countries');
...而且...
countriesRef = database.ref('/countries/');
...为了更加确定,我已经从firebase中复制/粘贴了名称。
这是数据库的屏幕截图...
Firebase 提供 2 种不同类型的数据库:实时数据库和Firestore。
从屏幕截图中,我看到您正在使用 Firestore 数据库,但您正在连接到实时数据库。
首先,初始化 Firebase(在您的示例中很好):
firebase.initializeApp(config);
Run Code Online (Sandbox Code Playgroud)
然后,您需要连接到 Firestore:
// Initialize Cloud Firestore through Firebase
const db = firebase.firestore();
// Disable deprecated features
db.settings({
timestampsInSnapshots: true
});
Run Code Online (Sandbox Code Playgroud)
现在,当您连接时是您的第一个请求的时间:
const countriesRef = db.collection("countries");
countriesRef.get()
.then((snapshot) => {
snapshot.docs.forEach(doc => {
console.log(doc.data())
})
})
.catch((error) => {
console.log("Error getting countries:", error);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2187 次 |
最近记录: |