Organizing the axios response in Vue

Curious Inquiry

What could be the missing link or error in my coding that prevents a simple joke from displaying in my project?

Check out my work-in-progress demo on codepen


Example JSON response

https://i.sstatic.net/xe9Qc.png

HTML Structure

<div class="jokes" v-for="joke in jokes">
  <h2>{{ joke.setup }}</h2>
  <p>{{ joke.line }}</p>
</div>

JS Implementation with Babel

new Vue({
  el: '#app',
  data:{
    jokes: []
  },
  created(){
    this.GetJokes();  
  },
  methods: {
     GetJokes () {
      axios.get('https://08ad1pao69.execute-api.us-east-1.amazonaws.com/dev/random_joke')
      .then(response => {

          let joke = response.data[0];

          let apiInfo = {
            setup: joke.setup,
            line: joke.punchline
          };
          this.jokes.push(apiInfo)
      })
    }
  }
})

Answer №1

Looks like your response is not in array format. The solution is simple:

let joke = response.data

Once you make this change, your code should work smoothly.

You can also view the updated CodePen here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Trouble arises when attempting to retrieve the ID of the parent <ul> element using .parents() and .closest() in jQuery

Currently, I have 2 distinct <ul> containers with separate id's. Each of these contains a list of <li> elements. The first container is identified as <ul id="coaches-list">, while the second is labeled as <ul id="players-list"> ...

Obtaining the result from within the .then() block

Through the utilization of Google's API, I am successful in retrieving and displaying nearby places on the console. router.get('/', function (req, res, next) { // Locating nearby establishments googleMapsClient.placesNearby({ ...

I'm encountering difficulties parsing the JSON message from Azure Service Bus within my Node.js application

Here is my JavaScript code for receiving a message from Azure Service Bus: function receiveMessage(serviceBusTopic, serviceBusSubscriber, callback) { serviceBus.receiveSubscriptionMessage(serviceBusTopic, serviceBusSubscriber, { isPeekLock: true }, func ...

"Limit the display of the b-form-datepicker to only show the month and

Is there a way to customize the b-form-datepicker in order to only allow selection of the month and year? I've searched through bootstrap-vue but couldn't find an example. ...

Show a loading image after clicking on an image or button using JavaScript

I recently created an App using PhoneGap and JqueryMobile. Transitioning between multiple HTML pages by clicking on images seems to be causing slow loading times, leaving end users unaware of what's happening in the background. I am looking for a way ...

Tips for utilizing the jsPDF framework?

I am looking to convert my webpage into a PDF file. I attempted the following solution: var doc = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#printPDF').c ...

What steps can be taken to resolve the error ERROR TypeError: undefined is not an object when evaluating 'userData.username'?

.I need help fixing this error ERROR TypeError: undefined is not an object (evaluating 'userData.username') Currently, I am working on a small application where users are required to allow permission for their location in order to save their cit ...

Utilizing jQuery AJAX to Send an HTML Array to PHP

In my current HTML forms and jQuery AJAX workflow within the Codeigniter Framework, I've encountered a common issue that has yet to be resolved to suit my specific requirements. Here's the situation: HTML - The form includes an array named addre ...

Tips for verifying the real DOM element with React Enzyme

Is it possible to retrieve the real DOM node in order to query it using the DOM api instead of having to rely on enzyme's api? This feature comes in handy for special cases where I need to make assertions about the actual DOM node. ...

Error encountered while accessing a JSON object through jQuery that has been serialized on an ASP.NET .cs page

Hey there, I've been trying to run the code below by keeping it in a separate JavaScript file and referencing that file in all my .aspx files. Strangely enough, when I place this code directly in the script section of a .aspx file, it works perfectly ...

At what point does angular2 @output get resolved?

Click on this link Here is the HTML code snippet: <chart [options]="options" (load)="saveGraphInstance($event.context)"></chart> <div (click)="switchGraph()">switch</div> <div (click)="showLoadingForce()">show loadin ...

Building a Mongoose Schema with an Array of Object IDs: A Step-by-Step Guide

I created a user schema using mongoose: var userSchema = mongoose.Schema({ email: { type: String, required: true, unique: true}, password: { type: String, required: true}, name: { first: { type: String, required: true, trim ...

Learning the process of using JavaScript to extract data from a JSON file containing arrays

Currently grappling with the challenge of reading a JSON file using JavaScript. Unsure if my JSON file is in the correct format with arrays, but here is what I have. [ { "passageNumber":"2.3.1", "title":"Inside and out: A bronze ...

Displaying all divs when the checkboxes are unchecked

My code displays a product list with various details stored in different divs based on data attributes like brand, store, and more. A friend helped me develop a filter using checkboxes for record selection. However, I need to make a small modification to i ...

Error in IE11 when using vue.js, webpack, and babel due to syntax issue

I've encountered an issue with a demo that works fine in Chrome but fails in IE11. The error seems to be related to an evaluation function and is only a problem in IE11. You can see an example of the issue here (please view with IE11): Example site ...

Customizing the appearance of data labels in Chart.js version 4.2.1 with Vue

I am currently working with Vue and ChartJS, and I am looking to customize the style of my data labels. Specifically, I have 3 data labels and I would like to change the style of the second label from normal to bold. My First Attempt plugins: { legend ...

automatic site map creation tool within a vue js application

I am trying to generate a sitemap based on routes in my Vue.js project, but I have been unable to find a solution so far. I came across the vue-router-sitemap package on npm, but it doesn't provide any examples and has left me completely confused. Wha ...

What is the best way to display "No results found" in Mat-select-autocomplete?

I am working with the mat-select-autocomplete for a multiselect dropdown. When searching for values that are not in the list, I want to display a message saying "No results found". Can someone please help me achieve this? Link to Code ...

Ensuring that objects remain fixed in place regardless of window resizing is a common task in web development, often achieved through a

I've been attempting to create range sliders with boxes that display the slider's current value, but I'm having trouble getting them positioned correctly when the window size changes. Despite trying various solutions found online, I resorted ...

When an element is dragged within the mcustomscrollbar container, the scroll does not automatically move downward

I am facing an issue where I have multiple draggable elements inside a Scrollbar using the mcustomscrollbar plugin. When I try to drag one of these elements to a droppable area located below the visible area of the scroller, the scroll does not automatical ...