Firebase @DocumentId annotation in javascript

Viewed 67

I'm developing Android app using Kotlin and Chrome Extension using plain HTML, CSS, JavaScript.

In Kotlin I use this @DocumentId annotation in my model class, it helps me get an object with a parameter id, because the id is key of the document in Cloud Firestore.

data class Word(
    @DocumentId
    val id: String = "",
    var term: String = "",
    var translation: String = "",
    var bookmark: Boolean = false,
    var reviewInterval: Int = 1
) 

Then I use this code in JavaScript. But how to use @DocumentId annotation like in previous example. JavaScript doesn't support annotation and I haven't found anything in docs. How to achieve the same result in JavaScript?

class Word {
    constructor(
        id = "",
        term = "",
        translation = "",
        bookmark = false,
        reviewInterval = 1
    ) {
        this.id = id;
        this.term = term;
        this.translation = translation;
        this.bookmark = bookmark;
        this.reviewInterval = reviewInterval;
    }
}
1 Answers

The @DocumentId annotation only works with code running in a JVM or Android. It doesn't work in JavaScript environments. Firestore doesn't have an equivalent construct for JavaScript, so you will have to write code to copy the ID of the document into the object after the query is complete.

Related