0

is it possible using an axios get request from Vuejs to make JSON calls to a firebase backend and return a list of all comments associated with a particular blog entry?

I want to basically say list all the comments where blogID = 23

In my comments table I can add a field blogID but can’t figure out how to pass a Param through a get request that relates to a field in a record called blogID.

Can anyone tell me if this can be done, share an example or point me at a tutorial or similar

Thanks

Paul
  • 581
  • 3
  • 10
  • 19

1 Answers1

1

Assumption: You are using the Real Time Database and not Firestore.

Yes you can use the Real Time Database REST API to do so, have a look at the documentation:

https://firebase.google.com/docs/database/rest/start?authuser=0

https://firebase.google.com/docs/database/rest/retrieve-data?authuser=0

Below is an example of how you would fetch comments data. Here, Axios is executed in a HTML page but you would use exactly the same code in a Vue.js method in a Vue component.

HTML Page:

<html>

<head>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>

<body>

<script>

   axios.get('https://projectname.firebaseio.com/blogItems/23/comments.json')
  .then(function (response) {

    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })

</script>

<body>
</html>

Real time database export:

{
  "blogItems": {
    "23": {
      "author": "John",
      "comments": [
        {
          "content": "Very good"
        },
        {
          "content": "Excellent post"
        }
      ],
      "content": "lorem ipsus ...."
    }
  }
}

However, using the Real Time Database REST API may not be the most efficient way of querying the Real Time Database from a Vue.js web-based application. It is better to use the standard JavaScript SDK. Is there any specific reason why you need to fetch data through Axios and not use the JavaScript SDK?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • Hi, many thanks, I am just doing this to learn about axios and JSON so I know how to create a separate front and backend where I can easily swap out firebase for something else if I want to, I just couldn’t see how to do this but I ll read...thanks – Paul Jul 27 '18 at 14:37
  • Ok, now I understand better :-) I've updated my answer with an example with Axios. If you think my answer has helped you, you may upvote it, thank you! – Renaud Tarnec Jul 27 '18 at 15:55
  • Would I post the data into separate tables with blog items being one and comments being a separate table with the blog item ID as the field I’d filter on? ( sorry for dB language). This looks like it would all go into one table in effect. I assumed I’d post to blog and then separately post comments linked to a blog..... is that incorrect? – Paul Jul 27 '18 at 21:43
  • 1
    There is no absolute answer to the question in your comment. There are several possibilities of modelling your data in the NoSQL Firebase Realtime Database. Have a look at those different posts/articles to read about the NoSQL data modelling "philosophy" and techniques. https://firebase.googleblog.com/2013/04/denormalizing-your-data-is-normal.html https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/ https://firebase.google.com/docs/database/web/structure-data https://stackoverflow.com/questions/16239819/performance-of-firebase-with-large-data-sets/16240601#16240601 – Renaud Tarnec Jul 28 '18 at 15:06