0

I'm trying to generate a random quote from data obtained through an api. I generated a random index but when I click on the corresponding button I'm not getting back a random quote. I'm not getting any apparent errors.

randomQuote() {
    const index = Math.floor(Math.random() * this.quotes.length);
    return this.quotes[index]
  },

Here's my data object.

data: {
    data: [],
    quotes: [],
    currentIndex: 0,
    currentPage: 1,
  },

Here's my axios call:

axios.get(url).then(res => {
        this.data = res.data;
        this.quotes = this.data;
      });
2
  • 1
    What does the <button> look like? I'd suggest at least pasting in the .vue file, or if possible a full repro in Codesandbox Commented Oct 19, 2021 at 23:13
  • @tony please show us what the markup for the button looks like and the method it is calling. The above tells us nothing Commented Oct 20, 2021 at 3:34

1 Answer 1

1

There is an apparent error in your axios call, you are not setting quotes to the correct object:

axios.get(url).then(res => {
  this.quotes = res.data.quotes;
});
Sign up to request clarification or add additional context in comments.

3 Comments

I'm able to get all the quotes with the code I have. My issue is when I click on the generate random button it doesn't work. So it looks like I am setting it to the correct object.
@tony Can you share a link to a reproduction?
@tony Definitely not with the code you have attached to your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.