[GH-ISSUE #229] ref().get() works while query().get() returns an empty array #106

Closed
opened 2026-05-23 08:30:56 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @JiPaix on GitHub (May 29, 2023).
Original GitHub issue: https://github.com/appy-one/acebase/issues/229

This might just be me not understanding how AceBase works but i'm getting some weird results between query() and ref()
A brief example of what's happening

const test1 = await db.ref(`users/user_1`).get()
const test2 = await db.query(`users/user_1`).get()

console.log(test1.val()) //=> { username: 'user_1' }
console.log(test2.getValues()) //=> DataSnapshotsArray(0) []

Here's the whole context:

export class UserDatabase extends AceBase {
  static #instance: UserDatabase

  static async getInstance (): Promise<UserDatabase> {
    if (!(this.#instance instanceof UserDatabase)) {
      this.#instance = new this('auth', { logLevel: 'log', storage: { path: '.' } })
      await this.#instance.ready()
      await this.#instance.#userSchemasAndIndexes()
    }
    return this.#instance
  }

  async #userSchemasAndIndexes (): Promise<void> {
    await this.schema.set('users/$uid', {
      username: 'string',
      password: 'string',
      createdAt: 'Date',
      allowed: 'boolean'
    })

    await this.indexes.create('users', 'username')
  }

  static async isAllowed (userId: string): Promise<boolean> {
    const self = await UserDatabase.getInstance()

   /**
    * Ideally this is what i'd like to do:
    * return self.query(`users/${userId)`).filter('allowed', '==', true).exists()
    */

    const ref = await self.ref(`users/${userId}`).get()
    const query= await self.query(`users/${userId}`).get()

    console.log(bb.getValues()) //=> empty array
    console.log(ref.val()) //=> the user obj


    return false //=> false for testing purpose
  }

Then somewhere else in the code is called UserDatabase.isAllowed

const allowed = await UserDatabase.isAllowed('1234')
Originally created by @JiPaix on GitHub (May 29, 2023). Original GitHub issue: https://github.com/appy-one/acebase/issues/229 This might just be me not understanding how AceBase works but i'm getting some weird results between `query()` and `ref()` A brief example of what's happening ```js const test1 = await db.ref(`users/user_1`).get() const test2 = await db.query(`users/user_1`).get() console.log(test1.val()) //=> { username: 'user_1' } console.log(test2.getValues()) //=> DataSnapshotsArray(0) [] ``` Here's the whole context: ```ts export class UserDatabase extends AceBase { static #instance: UserDatabase static async getInstance (): Promise<UserDatabase> { if (!(this.#instance instanceof UserDatabase)) { this.#instance = new this('auth', { logLevel: 'log', storage: { path: '.' } }) await this.#instance.ready() await this.#instance.#userSchemasAndIndexes() } return this.#instance } async #userSchemasAndIndexes (): Promise<void> { await this.schema.set('users/$uid', { username: 'string', password: 'string', createdAt: 'Date', allowed: 'boolean' }) await this.indexes.create('users', 'username') } static async isAllowed (userId: string): Promise<boolean> { const self = await UserDatabase.getInstance() /** * Ideally this is what i'd like to do: * return self.query(`users/${userId)`).filter('allowed', '==', true).exists() */ const ref = await self.ref(`users/${userId}`).get() const query= await self.query(`users/${userId}`).get() console.log(bb.getValues()) //=> empty array console.log(ref.val()) //=> the user obj return false //=> false for testing purpose } ``` Then somewhere else in the code is called `UserDatabase.isAllowed` ```ts const allowed = await UserDatabase.isAllowed('1234') ```
Author
Owner

@appy-one commented on GitHub (May 30, 2023):

ref references a specific entry in your database, where query searches an object collection by matching each child of the referenced path for given filter conditions.

You can simply use ref:

const snapshot = await db.ref(`users/${userId}/allowed`).get();
const isAllowed = snapshot.val() === true;

If you want to query all users that have allowed set to true, use:

const allowedUsers = await db.query('users').filter('allowed', '==', true).get();

Kindly note that instead of rolling your own auth, you might want to use AceBase server's built-in auth. See https://github.com/appy-one/acebase-server and https://github.com/appy-one/acebase-client

Do you like AceBase? 👇🏼

Sponsor AceBase Spread the word contribute

<!-- gh-comment-id:1568835780 --> @appy-one commented on GitHub (May 30, 2023): `ref` references a specific entry in your database, where `query` searches an object collection by matching each child of the referenced path for given filter conditions. You can simply use `ref`: ```js const snapshot = await db.ref(`users/${userId}/allowed`).get(); const isAllowed = snapshot.val() === true; ``` If you want to query all users that have `allowed` set to `true`, use: ```js const allowedUsers = await db.query('users').filter('allowed', '==', true).get(); ``` Kindly note that instead of rolling your own auth, you might want to use AceBase server's built-in auth. See https://github.com/appy-one/acebase-server and https://github.com/appy-one/acebase-client Do you like AceBase? 👇🏼 [![Sponsor AceBase](https://user-images.githubusercontent.com/26569719/168233053-8e56b243-4140-40ab-9a30-4cb3cc149bfe.svg)](https://github.com/sponsors/appy-one) [![Spread the word](https://user-images.githubusercontent.com/26569719/169265089-3d593555-e1ad-4390-986b-877ac2c38a47.svg)](https://twitter.com/intent/tweet?button=&url=https://github.com/appy-one/acebase&text=I'm+using+@AcebaseRealtime+in+my+project+to+make+my+life+easier!&button=) [![contribute](https://user-images.githubusercontent.com/26569719/169265318-30c4c6a5-7c89-46a0-a7a2-ef433a8192f4.svg)](https://github.com/appy-one/acebase#contributing)
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/acebase#106
No description provided.