admin管理员组

文章数量:1356789

I am trying to add a unique ID to certain objects to item information that I am inputting in the real-time database.

here is my code that I am trying to implement.

        database.ref('item/' + /*Here is where I would add the uniqueUD*/ ).set({
          ItemName: ItemName,
          Address: ItemAddress,
          Industry: ItemIndustry,
          Speciality: ItemSpeciality,
          Description: ItemDescription
        })

I am trying to have the database look like.

(ItemIndex) - (Item ID) - ItemName, ItemAddress, ItemIndustry, ItemSpeciality, ItemDescription

I am trying to add a unique ID to certain objects to item information that I am inputting in the real-time database.

here is my code that I am trying to implement.

        database.ref('item/' + /*Here is where I would add the uniqueUD*/ ).set({
          ItemName: ItemName,
          Address: ItemAddress,
          Industry: ItemIndustry,
          Speciality: ItemSpeciality,
          Description: ItemDescription
        })

I am trying to have the database look like.

(ItemIndex) - (Item ID) - ItemName, ItemAddress, ItemIndustry, ItemSpeciality, ItemDescription

Share Improve this question asked Jan 10, 2019 at 2:33 Jaffer Abyan SyedJaffer Abyan Syed 1291 gold badge2 silver badges11 bronze badges 2
  • You could use UUID – Andreas Commented Jan 10, 2019 at 2:40
  • @Andreas how would i implement that in the code for instance ? item.uuid – Jaffer Abyan Syed Commented Jan 10, 2019 at 2:49
Add a ment  | 

3 Answers 3

Reset to default 5

Firebase will create an ID for you if you use push instead of set:

    database.ref('item').push({
      ItemName: ItemName,
      Address: ItemAddress,
      Industry: ItemIndustry,
      Speciality: ItemSpeciality,
      Description: ItemDescription
    })

See docs: https://firebase.google./docs/database/admin/save-data#section-push

A reference's .push() method creates a new reference even if you don't pass any value to it. From the docs:

If you don't pass a value, nothing will be written to the Database and the child will remain empty (but you can use the Reference elsewhere).

This means you can create a new reference using this method without having to write anything to the db:

const ref = database.ref('item').push();

You can then use Firebase's generated unique ID from ref.key. You can include this value in your data if you want. When you are ready to write data to the same ref, you can call its .set() method.

ref.set({
    ItemName: ItemName,
    Address: ItemAddress,
    Industry: ItemIndustry,
    Speciality: ItemSpeciality,
    Description: ItemDescription
});

2023 v9 modules syntax example of how to get the ID first:

const itemRef = ref(database, `users/${userId}/items`)
const addedItem = await push(itemRef)
console.log(addedItem.key, "This is the ID you're looking for")

await set(addedItem, {
    ...{ exampleData: 'example' },
    id: addedItem.key, // You could insert the same ID into the item if you wanted to.
})

本文标签: javascripthow to make an id in a realtime databaseStack Overflow