Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

Firestore specific data read does not work with variable but works with static value

I am trying to query specific document from firestore database. The problem seems to be that If I add the doc(id) statically, it works but with variable it does not even tho the variable has correct and exact same value I tested statically with.

The document I am trying to retrieve is a User node/document under /users collection.

read is the function I am using to retrieve the data:

export default class GenericDB {
    constructor(collectionPath) {
      this.collectionPath = collectionPath
    }
    /**
       * Read a document in the collection
       * @param id
    */
    async read(id) {
      const result = await (await firestore())
        .collection(this.collectionPath)
        .doc(id)
        .get()

      const data = result.exists ? result.data() : null

      if (isNil(data)) return null

      this.convertObjectTimestampPropertiesToDate(data)
      return { id, ...data }
    }
}

This is my vuex action:

getUser: ({ commit }, userId) => {
    return new Promise((resolve, reject) => {
        //usin UsersDB() instead of Generic() because my UsersDB() has constructor with correct path to /users
        new UsersDB().read(userId).then(user => {
            //Empty user if userId value is from variable and not empty if I use static value
            resolve(user)
        })
    })
}

And I do call it out:

mounted() {
  if (this.id) {
      //getUser function is declared inside ...mapActions('authentication', ['getUser'])
      this.getUser(this.id)
  }
}

Update 1: I did compare static string against my variable with logical operator and it turns out that the variable userId has space at the end. I have no clue why and where does it come.

There is no error just empty data. I can not see what can be wrong with this simple query. Any help is appreciated!

Upvotes: 0

Views: 404

Answers (3)

heypran
heypran

Reputation: 146

I was facing the same issue recently. Then I figured out that my string has quotes. The following code solved the problem.

roomId.trim().replace(/['"]+/g, '')

Upvotes: 0

Tarvo Mäesepp
Tarvo Mäesepp

Reputation: 4533

It turned out that the userId was not exatctly the same if I compared them with logical operator. The variable version had space at the end.

So the solution was to use userId.replace(/\s/g, '')

Upvotes: 0

Vipulw
Vipulw

Reputation: 1283

Try making connection this way instead of directly using it.

const db = firebase.firestore();
async function read(id) {
    const result = await db
        .collection(this.collectionPath)
        .doc(id)
        .get()

    const data = result.exists ? result.data() : null

    if (isNil(data)) return null

    this.convertObjectTimestampPropertiesToDate(data)
    return { id, ...data }
}

Generally, standard format we mostly use to get document is :

const db = firebase.firestore();

const result = await db
    .collection("collection_name")
    .doc("document_id")
    .get();

I hope this helps you. Please let me know for any issues.

After you edited the question I tried passing a valid variable and I am getting response. This is giving me data of document.

//Firebase
const admin = require("firebase-admin");
let serviceAccount = require("./firebase.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore();
//End of Firebase

id = "lWxkvqZnBxNRke4SFyJj"
async function getData(id) {
    const result = await db
        .collection("users")
        .doc(id)
        .get();

    data = result.data()
    console.log(data)
    return data
}
getData(id)

Upvotes: 1

Related Questions