I have Firebase Realtime Database data structured as below where I want to replace "-preliminaryId" with "-ultimateId123" in the "contacts" and "notes" nodes. To perform this I will delete the old nodes and replace them with new nodes.
"prel":{
"-preliminaryId": {
"email" : "hello@bye.com"
}
},
"contacts": {
"-ABC": {
"-BCD": {
"email": "bye@nowhere.com",
"uid" : "-123456WWWXYz"
},
"-preliminaryId": {
"email": "hello@bye.com",
"uid": "-preliminaryId"
}
}
},
"notes": {
"-noteIdone": {
"access": {
"members": {
"-preliminaryId": 1
}
}
},
"-noteIdtwo": {
"access": {
"members": {
"-realId1234": 1
}
}
}
}
The new id for email "hello@bye.com" should be "-ultimateId123". (I cannot store email as keys in Firebase Realtime Database.)
I begin by fetching the preliminary id to replace.
const prelSnap = await dbRoot.child('prel').orderByChild('email').equalTo('hello@bye.com').once('value')
const prelData= prelSnap.val()
console.log('prelData', prelData)
const oldId = Object.keys(prelData)[0]
The prelData
variable will be
{ -preliminaryId: { email: 'hello@bye.com' } }
However, I would prefer it to just be
{ email: 'hello@bye.com' }
so I can get oldId
by prelSnap.key
instead of Object.keys(prelData)[0]
.
However, my real hurdle is fetching contacts
const contactSnaps = await dbRoot.child('contacts').orderByChild(`${oldId}/uid`).equalTo(oldId).once('value')
console.log('contactSnaps', contactSnaps.numChildren())
contactSnaps.forEach((contact)=>{
console.log('contactData', contact.val())
const userId = Object.keys(contactData)[0]
...
// Replace the contact node
myPromises.push(dbRoot.child(`contacts/${userId}/${newId}`).set(contactObj)) // Create new contact data
myPromises.push(dbRoot.child(`contacts/${userId}/${oldId}`).remove()) // Delete old contact
...
})
This will get me ALL contacts. The logout from console.log('contactData', contact.val())
will be:
numChildren 1
contactData {
"-BCD": {
"email": "bye@nowhere.com",
"uid" : "-123456WWWXYz"
},
-preliminaryId: {
"email": "hello@bye.com",
"uid": "-preliminaryId"
}
}
Hardcoded example:
const contactSnaps = await dbRoot.child('contacts').orderByChild('-preliminaryId/uid').equalTo('-preliminaryId').once('value')
console.log('contactSnapsHC', contactSnaps.numChildren())
contactSnaps.forEach((contact)=>{
const contactData = contact.val()
console.log('contactDataHC', contactData)
})
Output from above hardcoded example:
contactSnapsHC 1
contactDataHC { -BCD: { email: "bye@nowhere.com", uid : "-123456WWWXYz" }, -preliminaryId: { email: "hello@bye.com", uid: "-preliminaryId" } }
My guess is that this might be somewhat related to my issue with the first query?
When performing the same type of query on notes, all works well though.
nodeSnaps = await dbRoot.child('notes').orderByChild(`access/members/${oldId}`).equalTo(1).once('value')
console.log('notes', nodeSnaps.numChildren())
nodeSnaps.forEach((node)=>{
console.log('notesData', node.val())
...
}
The output here will be:
notes 1
notesData { "access": { "members": { "-preliminaryId": 1 } } }
I expected the first two queries to return result at the same node level as the notes
query.
How can I query the database to return the result shown below?
prelData { email: 'hello@bye.com' }
contactData { email: 'hello@bye.com', uid: '-preliminaryId' }
Kind regards /K