简体   繁体   English

我不知道如何使用 Firebase 返回的 object 获取查询

[英]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.

相关问题 无法弄清楚MYSQL查询以获得我想要的结果 - Can't figure out MYSQL query to get the result I want 我不知道如何使我的javascript onclick事件正常工作 - I can't figure out how to get my javascript onclick event to work 我不知道如何让这些颜色变量在绘图 function 中工作 - I can't figure out how to get these color variables to work in the draw function clearInterval()不起作用,我无法弄清楚原因 - clearInterval() doesn't work and I can't figure out why 无法弄清楚如何在 firebase 中编写正确的安全规则 - Can't figure out how to write proper security rules in firebase .find在jQuery 1.4.2中不起作用,我不知道如何支持它 - .find doesn't work in jquery 1.4.2 and I can't figure out how to support it 我是 firebase 的新人,我正在尝试获取父集合的 ID 文档,但我无法弄清楚 - I'm new in firebase and I'm trying to get id document for parent collection but I can't figure it out 无法弄清楚如何使用AJAX从JSON数据对象获取特定值 - Can't figure out how to get from a JSON data object a specific value with AJAX 无法弄清楚如何使用jQuery形式和.length - Can't figure out how to work with jQuery form and .length 我只是无法弄清楚如何检索传递的对象的名称和值 - I just can't figure out how to retrieve the name and value of the object passed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM