简体   繁体   中英

Firebase Realtime Database - orderByChild().equalTo() does not return expected node

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

I think your confusion is about what the query returns here:

dbRoot.child('contacts').orderByChild('-preliminaryId/uid').equalTo('-preliminaryId')

Firebase queries operate on a flat list. Since you query contacts , the nodes returned will be child nodes of contacts . Specifically, it returns any child node of contact for which a property -preliminaryId/uid exists, which has a value of -preliminaryId . In your example that is true for the node at contacts/-ABC , which is what the query returns for me.


For example, when I add another sibling node on the same level as ABC :

"contacts": {
  "-ABC": {
     "-BCD": {
       "email": "bye@nowhere.com",
       "uid" : "-123456WWWXYz"
     },
     "-preliminaryId": {
       "email": "hello@bye.com",
       "uid": "-preliminaryId"
     }
  },
  "DEF": {
     "-HKI": {
       "uid": "another id"
     }
  }
},

Your query in this case still only returns the -ABC node, since that's the only one where a -preliminaryId/uid exists with the correct value.

For a working example of this, see: https://jsbin.com/wivukeq/edit?js,console

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM