I am interested in retrieving a particular item from the data() function in Firestore

snapshot.forEach(doc => {
  console.log("ID: "+doc.id, '=>', "Doc DATA: "+JSON.stringify(doc.data()));
});

I am looking to extract just one item from doc.data(), which is an array of strings named "supportedCurrencies". Can someone guide me on how to do this? I am still learning about using Firestore and TS, so any assistance would be greatly valued.

Thank you!

Answer №1

Using doc.data() function retrieves a plain JavaScript object with fields corresponding to those in the document:

const data = doc.data();

To access a specific field, simply refer to it by name as a property of the object:

const supportedCurrencies = data.supportedCurrencies;

If the field is an array, it can be manipulated using standard JavaScript array methods.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize information from a JSON Array to populate a JavaScript Array

Is there a way to link the data from the $data variable in ajax.php to the this.products[] array in store.js? Both files are separate, so how can I achieve this? The webpage displays the database data returned by ajax.php as shown below: [{"0":"100001"," ...

Testing NextJS App Router API routes with Jest: A comprehensive guide

Looking to test a basic API route: File ./src/app/api/name import { NextResponse } from 'next/server'; export async function GET() { const name = process.env.NAME; return NextResponse.json({ name, }); } Attempting to test ...

What is the best way to delay an observable from triggering the next event?

In my Angular project, I am implementing RxJs with two subjects. s1.next() s1.subscribe(() => { // perform some operation and then trigger the event for s2 s2.next() }); s2.subscribe(() => { // perform some operat ...

A custom function that changes the state of a nested object using a specified variable as the key argument

My goal is to update the state of an object using a function called updateDatas. This function takes the arguments key, possibly subKey, and value. interface Datas { code: number; item1: Item; } interface Item { id: number; name: string; } const ...

Access-Control-Allow-Methods does not allow the use of Method PUT in the preflight response, as stated by Firebase Cloud Functions

I am facing an issue with my Firebase cloud function endpoint. I have a setup where it forwards PUT requests to another API endpoint. I have configured the following Access-Control-Allow- headers: // src/middlewares/enableCORS.ts export default function en ...

Using multiple MaterialUI components in a JavaScript page with React can pose challenges

Incorporating multiple MaterialUI Cards in my project, I encountered an issue where all the cards would expand or the select values would change simultaneously. Despite using unique key values and mapped components with distinct keys, the problem persisted ...

What is the best way to effectively use combinedLatestWith?

https://stackblitz.com/edit/angular-ivy-s2ujmr?file=src/app/country-card/country-card.component.html I am currently working on implementing a search bar in Angular that filters the "countries$" Observable based on user input. My approach involves creatin ...

AngularJS's $http.get function has the ability to read text files, but it struggles with reading JSON

I'm new to angularjs and I am struggling to retrieve data from a json file using the $http.get method. It seems to work fine when I try to read a simple txt file, but not with the json file. I can't seem to pinpoint where the issue lies... Belo ...

What is the best way to deserialize certain fields from a Json object using Gson?

Here is the JSON string I have: [ { "channel": "/bvmt/initValues", "data": { "value": { "instrumentIds": "['TN0007250012','TN0007500010']", "instruments": "[{'mnemonic':'ADWYA',&apos ...

Identified the category

How can I retrieve the default option from a list of options? type export type Unpacked<T> = T extends Array<infer U> ? U : T; interface getDefaultValue?: <T extends Option[]>(options: T) => Unpacked<T>; Example const options = ...

Exploring Ruby's hash and array features

Within my Rails application, I am tasked with sending a complex JSON string that requires decoding. While this is not an issue, I want to access sample JSON structures beforehand to ensure I can navigate through all the variables. The challenge lies in the ...

What is the best way to identify modifications in a JSON object?

import QtQuick 2.12 import QtQuick.Window 2.12 import QtQuick.Controls 2.0 Window { visible: true width: 640 height: 480 property var json: { "name": "old" } Component.onCompleted: json.name = "new" onJsonChanged: co ...

Verify enum values within controller function

I am dealing with a query parameter in my REST API that should be restricted to specific values according to an enum type. I need to find a way to handle a "Bad Request" error if the client provides any value outside of this enum. Here is what my enum loo ...

Navigating with the Angular router leads to an unexpected destination: "mat-radio-group-0=true"

I'm facing an issue with a close button in my HTML template that triggers a close() function in the component: HTML template: <div> <label id="radio-group-label">Please specify: </label> <mat-radio-grou ...

Styling with CSS in Angular 2+ can be quite challenging

Hey there, I'm new to Angular 4 and running into some troubles with styling my application. I tried adding a background image to the body, which worked fine, and then added a component to display content, also looking good. Now, when I added a second ...

How can I handle JSON data that contains undefined values?

Similar Question: Is it possible to parse JSON with special 'undefined' values using JavaScript? I'm curious if there's a way to parse something like javascript JSON.parse('{ "name": undefined}'); that is generated by an API ...

When JSON data is displayed in an HTML table, it mysteriously duplicates

I was able to retrieve the data using this code, but I am encountering an issue where the data is being displayed multiple times for certain entries. For example, "tom" might appear as tom, tom, tom... <div class="row"> <div class="col-12"> ...

Mouse event listener includes timeout function that updates a global variable

I'm currently working on a function that gets activated by a mouse click, but there's a 10-second cooldown period using setTimeout() before it can be triggered again. However, after the timeout, my variable doesn't get set to the correct boo ...

The ElasticClient encountered a problem while verifying the connection status

I am currently attempting to verify the status of a connection, but encounter an error during the check. var node = new Uri("http://myhost:9200"); var settings = new ConnectionSettings(node); ElasticClient client = new ElasticClient(settings); ISt ...

picker elementClass()

I encountered an issue while using the datepicker from Material UI. The datepicker has a method called dateClass: dateClass() { return (date: Date): MatCalendarCellCssClasses => { const unvailableArray = this.shareDate.unavailableDates; ...