[英]I can't figure out how to work with the object returned by Firebase get query
I'm building a React Native app with Expo and integrating Firebase (real time database) which I know has some limitations ( https://docs.expo.dev/guides/using-firebase/ ). I'm building a React Native app with Expo and integrating Firebase (real time database) which I know has some limitations ( https://docs.expo.dev/guides/using-firebase/ ). I'm trying to get a snapshot of the data using await get(query(...
and have successfully done so, but I can't figure out exactly what I'm working with. When I console.log
it I get this:我正在尝试使用await get(query(...
并已成功完成此操作,但我无法弄清楚我正在使用什么。当我console.log
它时,我得到了这个:
Object {
"key1": value1,
"key2": value2,
}
I was trying to return an array of the keys using Object.keys()
but it returns this:我试图使用Object.keys()
返回一个键数组,但它返回:
Array [
"_node",
"ref",
"_index",
]
This doesn't line up with the examples of Object.keys()
I'm seeing on the internet which makes me think this isn't a JSON object like I thought it was.这与我在互联网上看到的Object.keys()
的示例不符,这让我认为这不是 JSON object 就像我想的那样。 I've tried poking around a good bit with some other stuff but can't figure it out.我已经尝试过用其他一些东西四处寻找,但无法弄清楚。 The problem is, when I use typeof
on the object, it simply returns 'object' which is a little too vague for me to take to the google machine.问题是,当我在 object 上使用typeof
时,它只是返回“对象”,这对我来说太模糊了,无法带到谷歌机器上。
The following is a representation of my code.以下是我的代码的表示。 Thanks for your help.谢谢你的帮助。
import { initializeApp } from 'firebase/app';
import { get, getDatabase, query, ref } from 'firebase/database';
const firebaseConfig = {
databaseURL: '<myURL>',
projectId: '<myID>',
};
const app = initializeApp(firebaseConfig);
export default async function myFunction() {
const db = getDatabase(app);
const readReference = ref(db, '/<I am only reading a piece of the data>')
const existingData = await get(query(readReference))
const dataKeys = Object.keys(existingData)
console.log(dataKeys)
console.log(existingData)
console.log(typeof existingData)
}
What you get back from Firebase is known as a DataSnapshot
, and contains the JSON of the location you read, and some more metadata.您从 Firebase 返回的内容称为DataSnapshot
,其中包含您读取的位置的 JSON 以及更多元数据。
If you just want to get the JSON value of the snapshot, use snapshot.val()
as shown in the Expo documentation for Storing Data and Receiving Updates .如果您只想获取快照的 JSON 值,请使用snapshot.val()
,如存储数据和接收更新的 Expo 文档中所示。
Something like:就像是:
const existingData = await get(query(readReference))
const dataKeys = Object.keys(existingData.val())
console.log(dataKeys)
console.log(existingData.val())
console.log(typeof existingData.val())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.