0

I am trying to call the relevent data from Firebase based on the paramater in the URL.

For example:

var userId = 'EqBOy1RVZacBCaNlzbG9NDHrXl23'

//sets the variable to the exercise name in the url
var exerciseName = this.$route.params.exercise_name //'dumbbell_extension'for example

db.collection('track').doc(userId).get().then((doc) => {
  if (doc.exists) {
      this.exerciseData = doc.data().exerciseName //Returns 'undefined'
  }
})

Here is a picture of the database: Firebase Document Image

This returns undefined despite of the data existing in Firebase and everything else working as intended. I presume this is because it is actually searching for a field called exerciseName instead of the variable exerciseName.

enter image description here

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Please edit the question to all the missing data here. We can't see the value of `this.userId`, nor can we see the document in the database that's supposed to match your query. You're probably doing something wrong, but without seeing all the information, there's nothing we can do. – Doug Stevenson Mar 30 '20 at 18:36
  • Updated it, thanks. I know for sure it isnt a problem with the user id. Though I have added it in the code snippet to help clarify. – Bob Johnson Mar 30 '20 at 19:14

2 Answers2

3

The data() method of a DocumentSnapshot "retrieves all fields in the document as an Object".

You should therefore use the square bracket notation, which allows you to access the Object properties by name, as follows:

var userId = 'EqBOy1RVZacBCaNlzbG9NDHrXl23'

//sets the variable to the exercise name in the url
var exerciseName = this.$route.params.exercise_name //'dumbbell_extension'for example

db.collection('track').doc(userId).get().then((doc) => {
  if (doc.exists) {
      this.exerciseData = doc.data()[exerciseName] 
  }
})

You may also be interested by this answer https://stackoverflow.com/a/4968460/7015400, which gives more details about the square bracket notation.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

Your code is looking for a field called "exerciseName" in the document, but the document has no such field. The only field in your document, as shown in the screenshot, is called "dumbbell_extension", and we can't see what it contains (it could be a list or map).

Since we don't know what your code is expecting to do, there's no much else to be said. Your code needs to use the names of the fields that are present in the document, or check to see if they actually exist before using them.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441