Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can not get id for each item in the collection with useCollectionData hook #311

Open
feelforyou opened this issue Aug 17, 2023 · 2 comments

Comments

@feelforyou
Copy link

When i try to debug and console.log (listings) or console.log(car)
I dont get id for each item, except id i can get every field that is in my collection.
I think {idField:'id:} is not grabbing id from the collection

As a side note I am using firebase 9, installed react-firebase-hooks @latest , created
Vite react project @latest

simple example:

import React from "react";
import { useGlobalContext } from "../../../Context/Context";
import { db } from "../../../FirebaseConfig/firebaseConfig";
import { collection, query, where } from "firebase/firestore";
import { useCollectionData } from 'react-firebase-hooks/firestore';
import CarListing from "./CarListing";

const Listings = () => {
  const { user } = useGlobalContext();

  const carsRef = collection(db, "cars");
  const q = query(carsRef, where("addedByUID", "==", user?.uid));
  const [listings, loading, error] = useCollectionData(q, { idField: 'id' });

  if (loading) {
    return <div className="loading"></div>;
  }

  if (error) {
    console.error("Error fetching user listings: ", error);
    return <div>Error fetching data</div>;
  }

  return (
    <div className="listing-car-container">
      {listings && listings.length > 0 ? (
        listings.map((car) => <CarListing key={car.id} car={car} />)
      ) : (
        <p>You haven't listed any cars yet.</p>
      )}
    </div>
  );
};

export default Listings;
@tigerxy
Copy link

tigerxy commented Jan 2, 2024

You can append the id to your documents with the FirestoreDataConverter. You will receive the id in the fromFirestore function with the snapshot.id value. An example how to do this is in the documentation under transforming-data

@syuabov
Copy link

syuabov commented Mar 16, 2024

@feelforyou did you ever get the { idField: 'id' } to grab the id from the doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants