Retrieve the document's reference by utilizing the username

I am currently attempting to retrieve a document reference from Firestore using the code below:

var sfDocRef = database.collection("tags").doc("567BHJV6JF95HVCF44HB");

When I hover over sfDocRef, it displays:

(local var) sfDocRef: FirebaseFirestore.DocumentReference<FirebaseFirestore.DocumentData>

However, my goal is to obtain the address of the document based on its name instead of its ID. To achieve this, I am utilizing the following code:

var sfDocRef = database.collection("tags").where("userName", "==", userName);

Upon hovering over this code, the displayed datatype is as follows:

(local var) sfDocRef: FirebaseFirestore.Query<FirebaseFirestore.DocumentData>

I am in need of the DocumentReference datatype using the username instead of the ID. Is there a way to accomplish this in a single line? I have not found any relevant information in the documentation. Any assistance would be greatly appreciated.

Answer №1

When running a query, keep in mind that it can yield multiple results. This means that instead of receiving just one DocumentReference, you will actually get back a Query object that points to several documents.

If you wish to access the actual document reference(s), you need to execute the query and iterate over the results as shown below:

var query = database.collection("tags").where("userName", "==", userName);
query.get()
    .then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            // The data within the query's document is always defined
            console.log(doc.id, " => ", doc.data());
            // Retrieve the Document Reference using doc.ref
        });
    })
    .catch(function(error) {
        console.log("Encountered an error while retrieving documents: ", error);
    });

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

Looking to extract a particular set of information from past records in a GAS web application

Regarding a previous query I made before (Check for login username and password, and then bring data from every previous entry). I've developed a web application to monitor the working hours of employees at my organization. The app is straightforward ...

The word-break CSS property is ineffective in this situation

I've been experimenting with the word-break css property, but I just can't seem to get it to work even when using a simple example. Here's my code: React: render() { return ( <h5 className="word-break">A very very long line.... ...

Change the default direction of content scrolling in CSS and JavaScript to scroll upwards instead of

I am currently working on a website where the navigation bar spans the entire width and remains fixed in place. Below the navigation bar is a cover image, followed by the main content section. As you scroll down, the navigation bar covers the image from t ...

Adding a New Child Entry to an Existing Firebase Database: Step-by-Step Guide

Currently, I have a Firebase database set up with user child elements that represent dog owners within my application. I am exploring the possibility of adding a new dog under each owner in the future. Each dog would have unique details such as breed, weig ...

Moving Rows Between AgGrids

Experimenting with the example provided in this link (Dragging Multiple Records Between Grids) within my React application: https://www.ag-grid.com/react-data-grid/row-dragging-to-grid/ I have some inquiries; How can I retrieve the selected rows for the ...

The easiest high-order components combined with material-ui library

While using the TextField directly from my component (case 1) everything works perfectly fine. However, when I wrap it inside a function from props (case 2), it loses focus after each change. import TextField from 'material-ui/TextField'; var C ...

Unable to generate paths in Ionic 3

I am trying to view the actual route on the URL, but I'm having trouble figuring out exactly what needs to be changed. I've been referring to the documentation at: https://ionicframework.com/docs/api/navigation/IonicPage/ However, I keep encoun ...

What is the reason that textContent assignment does not function properly when used with HTML tags?

I have been attempting to insert additional text into an existing element using the textContent property. However, when I use the += operator for addition assignment, it seems to strip away the formatting of pre-existing tags like b. I expected that the ne ...

What is the process for integrating unit tests from external sources into an Angular project following an upgrade to version 15

As of Angular v15, the require.context function has been removed from the test.ts configuration file. I used to rely on require.context to expose tests outside of the Angular project to Karma. Now that it's no longer available: const contextGlobal = ...

Ways to utilize JavaScript to identify if a flash is launching in a separate tab

On my website, I have embedded a third-party flash player using an iframe. Whenever a user clicks on a specific area within the flash player, a new tab is opened in the browser. I am trying to track the frequency of this occurrence. However, I have encoun ...

Neglecting to Execute Success Function After Retrieving Data from PageMethods Using Multiple Parameters in C#

I attempted to trigger my OnSuccess function, but it did not execute on the server. This is my code: function Get_Data(option , text){ //returns 'data', 'data', --not call OnSuccess-- PageMethods.BindSeries(option,text,OnSuccess); ...

Using Jquery to fetch data with a specific port by using the $.getJSON method

Struggling to utilize $.getJSON in a scenario where my local app needs to call another local app on a different port. Specifically, my app is hosted on localhost:3000, but I'm attempting to use $.getJSON to request data from an app running on localhos ...

Using JavaScript import may encounter issues when trying to access the same file or a different file

When importing something and using it, there are certain scenarios where it may not work as expected. For example: <html> <body> <button onclick="foo()">Click Me</button> </body> <script type="module"> ...

JavaScript files and the power of jQuery AJAX

If I have a .js file with the code snippet below, how can I use a jQuery AJAX method to call it? This file resembles a JSON file in terms of syntax, which is causing some confusion for me. Is the JSON.stringify() method necessary for this task? Although ...

Click function for mat-step event handler

Is it feasible to create a click event for the mat-step button? I want to be able to add a (click) event for each mat-step button that triggers a method. Essentially, I am looking to make the mat-step button function like a regular button. You can find mo ...

Is it possible for me to connect one element to another using CSS or JavaScript?

On my website, there are three columns of interactive elements that can be scrolled vertically. Users are able to select one item from each column, and once selected, the chosen elements are connected by lines. The challenge I am facing is determining ho ...

Converting and saving geometry in three.js using the toJSON method and BufferGeometryLoader for serialization and deserialization. Transmitting geometries as text

Let me start by saying that I am new to three.js and would like to share my learning journey. My objective was to convert geometry into a format that can be transferred to another web worker (as not all objects can be transferred between workers, only s ...

Can you explain why Argument of type 'Element' cannot be assigned to a parameter of type 'never'?

The Chessboard Challenge export default function GenerateChessboard(){ let chessboard = []; for(let i = 0 ; i < rowsArray.length; i++) { for(let j = columnsArray.length-1 ; j >= 0 ; j--) { const number = i + j ...

Currently, I am developing a customized stylesheet specifically designed for Internet Explorer versions 10 and 11

Is it possible to utilize this straightforward script for identifying IE versions 10 and 11? if($.browser.version == 11.0 || $.browser.version == 10.0) { $("body").addClass("ie"); } ...

Encountering a Duplicate Types error while working with react, reactdom, and react-redux types simultaneously

I'm currently experimenting with using React-Redux in conjunction with TypeScript. Taking a look at my package.json, here's how it is structured: "dependencies": { "@types/react-dom": "15.5.0", "@types/react": "15.0.4", " ...