[Vuejs]-Firestore specific data read does not work with variable but works with static value

0👍

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, '')

1👍

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)
👤Vipulw

0👍

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, '')

Leave a comment