Home > Blockchain >  get the uuid in an object array with Firebase
get the uuid in an object array with Firebase

Time:08-19

Hello I would like to know if there would be a method to recover the uuid when I push data in my table I put you in the screenshot below

the code

push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
  name: registerInformation.nameBox,
  id: registerInformation.idBox,
  categories: registerInformation.categories,
  routine: registerInformation.routine,
});

screenshot

CodePudding user response:

The push function returns a Reference object, from which you can get the key with something like this:

const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`), {
  name: registerInformation.nameBox,
  id: registerInformation.idBox,
  categories: registerInformation.categories,
  routine: registerInformation.routine,
});
console.log(newRef.key);

If you want to use that key in the write operation, you can also separate the creating of the new ID from the writing of the data like this:

const newRef = push(ref(db, `/users/${auth.currentUser.uid}/user/sensors`));
console.log(newRef.key);
set(newRef, {
  name: registerInformation.nameBox,
  id: registerInformation.idBox,
  categories: registerInformation.categories,
  routine: registerInformation.routine,
});

In this snippet, the first line is a pure client-side operation that doesn't actually write anything to the database yet.

  • Related