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
3 Answers
Reset to default 5Firebase 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
版权声明:本文标题:javascript - how to make an id in a realtime database - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056992a2583435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论