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>