0

I have my code set up like this

const userCollectionRef = collection(db, 'users');

and then I create the data like this :

const addInfoToDataBase = async () => {
    checkLike();
    await addDoc(userCollectionRef, {
      likePost: userLikes,
      username: user,
      id: sale_id,
    });
  };

The way this is setup it generates users> random assigned id value > {id, username, likepost}

I'm trying to change that random assigned id value and assign it a dynamic item that I have called sale_Id. I tried doing this :

const userCollectionRef = collection(db, 'users', sale_id, 'id');

but this creates users> sale_id > id > random assigned id value > {id, username, likepost}

Is it possible to achieve users> sale_id > {id, username, likepost} ?

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
Mooch
  • 113
  • 1
  • 8
  • You'll want to use `setDoc` to specify an identifier. See [Add data to Cloud Firestore](https://firebase.google.com/docs/firestore/manage-data/add-data) – DazWilkin Apr 05 '22 at 01:56

1 Answers1

1

You could use setDoc for your use-case. See code below:

import { doc, setDoc } from "firebase/firestore"; 

// Adds a new document in the collection "users" which uses `sale_id` as document id.
await setDoc(doc(db, "users", sale_id), {
  id: sale_id,
  username: user,
  likePost: userLikes
});

For more information, you may check this documentation.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19