fiirebase result.val is null with await

user43286

What is the response structure of firebase queries? Is there any documentation? I couldn't find anything. I don't why user is null below. Do I need to run for loop and get the first item?

const result = await firebase.child('users').orderByChild('email')
    .equalTo(memberEmail).limitToFirst(1)
    .once('value');
const user = result.val();

the following code works, but I don't want to run a loop to get single value. How do I get the user without running a loop?

result.forEach((value) => {
          alert(`user = ${JSON.stringify(value.val())}`);
      });
Frank van Puffelen

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. So if you use a query, you will always need a loop.

The way to not require a loop is by directly accessing the child node that you are looking for. In your current data structure you likely store the users by their uid, so you can only access them directly by uid:

const result = await firebase.child('users').child(currentUser.uid).once('value');
const user = result.val();

If you'd store the users by their email address (you'll have to encode it, since a . cannot be used in the key), you could also access the users by their email address.

An idiomatic way to do this with Firebase is to keep a separate node that maps email addresses to uids. You'd then do two direct lookups:

  1. firebase.child('emailToUid').child(email)
  2. firebase.child('users').child(uid)

Each of those would return a single node (or no node if there is no match found), so you could chain the awaits and won't need a loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mixing async/await with Result

From Dev

async, await and strange result

From Dev

Unexpected Result when Overriding 'val'

From Dev

signed result of Val function in VBA

From Dev

Multiple select val() returning null

From Dev

How to avoid Await.result?

From Dev

jquery comparing string with .val(); non expected result

From Dev

jquery comparing string with .val(); non expected result

From Dev

Kotlin - wrong val result in class with secondary constructor

From Dev

Mask Text is missing, when Setting .val(null) or .val('')

From Dev

Shorhand for a = (a == val1) ? null : val1;

From Dev

Getting a null with a val depending on abstract def in a trait

From Dev

$(select).val().length of null gives error

From Dev

Task<TResult>.Result vs await a task

From Dev

Can the await keyword result in parallel code?

From Dev

Await.result or just a simple call?

From Dev

Await vs Task.Result in an Async Method

From Java

Await on a completed task same as task.Result?

From Dev

How to wait the result of async operations without await?

From Dev

await promise result before next loop iteration

From Dev

Will every 'await' operator result in a state machine?

From Dev

Unexpected result while using async await

From Dev

Can't seem to fix undefined await result

From Dev

C# using await versus result

From Dev

How to wait the result of async operations without await?

From Dev

Role of TimeOut and time passed to Await.result

From Dev

FunScript querySelector null result

From Dev

Collect only if result is not null

From Dev

GROUP result value if null