0

I can't figure out how to get a returned item from a firebase doc into a variable. I try the following and get the following error: Error getting sensor document: TypeError: Cannot set property 'node_addr' of undefined

I have the variable declared, and the line of code console.log("Document data:", doc.data()) does return the entire document object, so it does appear to be retrieving the document. The document does contain the field "node_addr" which is a number field that is populated on the target test document. I am trying to get this value into this.node_addr

This same sort of process does work if I query a collection searching for a specific value, but when I try to query a specific document with a known name, I get the error. I think I have something minor wrong with the syntax.

Please help. Thank you!

<script>
import db from '../components/firebaseInit'
export default {
    name: 'sensor-edit',
    data () {
        return {
            node_addr: 0,
            node_chan: 0,
            target_sensor_doc: ''
        }
    },
    created () {
        this.target_sensor_doc = this.$route.params.sensor_record.id

        var docRef = db.collection('sensors').doc(this.target_sensor_doc.toString())
        docRef.get().then(function(doc) {

                this.node_addr = doc.data().node_addr
                console.log("Document data:", doc.data())

        }).catch(function(error) {
            console.log("Error getting sensor document:", error);
        })
    }
}
</script>
kjav
  • 93
  • 1
  • 7
  • Please edit the question to show **exactly** the value of `this.target_sensor_doc.toString()` and the associated document that you're trying to get. – Doug Stevenson May 20 '20 at 23:30
  • Use arrow functions, ie `.then(doc => { this.node_addr = doc...` – Phil May 20 '20 at 23:35
  • Does this answer your question? [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – Phil May 20 '20 at 23:37
  • @Phil Thank you sir!! Your example of the arrow function with an actual example of the syntax did it. Unfortunately I don't understand what is posted in the link you shared. My brain can't connect MyConstructor to a Firestore query. :) You are my favorite person today. This ended days of frustration. Thanks again!! – kjav May 20 '20 at 23:59
  • To compare, `MyConstructor` is the equivalent of your Vue component and `transport.on()` is the equivalent of `docRef.get().then()` – Phil May 21 '20 at 00:13

1 Answers1

-1

Thank you @Phil !!!! This is exactly what I needed. I've been fighting with this for several days.

For anyone else needing this later.... here is the correct working query:

created () {
    this.target_sensor_doc = this.$route.params.sensor_record.id
    var docRef = db.collection('sensors').doc(this.target_sensor_doc.toString())
        docRef.get().then(doc => {
            this.node_addr = doc.data().node_addr
        })
}
kjav
  • 93
  • 1
  • 7