Чтобы поместить уникальный идентификатор в один и тот же уникальный идентификатор в базе данных Firebase Realtime, вы можете использовать следующие методы:
Метод 1: использование push()
и update()
const firebase = require('firebase');
// Initialize Firebase
firebase.initializeApp({
// Your Firebase configuration
});
// Generate a unique ID
const uniqueId = firebase.database().ref().push().key;
// Create a reference to the parent node
const parentRef = firebase.database().ref('parentUniqueId');
// Update the parent node with the unique ID
parentRef.update({ [uniqueId]: true })
.then(() => {
console.log('Unique ID pushed successfully.');
})
.catch((error) => {
console.error('Error pushing unique ID:', error);
});
Метод 2: использование set()
и child()
const firebase = require('firebase');
// Initialize Firebase
firebase.initializeApp({
// Your Firebase configuration
});
// Generate a unique ID
const uniqueId = firebase.database().ref().push().key;
// Create a reference to the parent node
const parentRef = firebase.database().ref('parentUniqueId');
// Create a reference to the child node with the unique ID
const childRef = parentRef.child(uniqueId);
// Set a value for the child node
childRef.set(true)
.then(() => {
console.log('Unique ID pushed successfully.');
})
.catch((error) => {
console.error('Error pushing unique ID:', error);
});
В обоих методах мы генерируем уникальный идентификатор с помощью push().key
, а затем используем его для обновления или установки нового дочернего узла внутри родительского узла.