During the rendering process, the property "quote" was accessed, however, it is not defined on the current instance. (Vue.js)

Every time I try to retrieve data from the kanye API, I encounter this error message:

Property "quote" was accessed during render but is not defined on instance.

Below is the code snippet that triggered the error:

<template>
  <div>
    <i>{{quote}}</i>
    <p>Kanye West</p>
    
  </div>
</template>

<script>
import axios from 'axios'
import { ref } from 'vue'
export default {

    setup() {

        const quote = ref('')

        const getQuote = async () => {
            const response = await axios.get('https://api.kanye.rest/')
            quote.value = response.data.quote
        }
        getQuote()

`

Answer №1

Don't forget to include the return statement for the quote in your setup function.

Check out the Live Demo below:

console.clear();

const { ref, onMounted } = Vue;

let options = {
  setup: function () {
    let quote = ref('');

    onMounted(function () {
      // This is just a demo using mock api response. You can replace it with actual API call.
      quote.value = 'Today\'s inspirational quote';
    });

    return {
      quote
    };
  }
};

Vue.createApp(options).mount('#app');
<script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="243316061c2b351b051b05283b3d39361c1316">[email protected]</a>/dist/vue.global.js"></script>
<div id="app">
  {{ quote }}
</div>

Answer №2

Simply retrieve the quote.

<template>
  <i>"{{ quote }}"</i>
  <p>Kanye West</p>
</template>

<script>
import axios from "axios";
import { ref } from "vue";
export default {
  setup() {
    const quote = ref("");

    const getQuote = async () => {
      const response = await axios.get("https://api.kanye.rest/");
      quote.value = response.data.quote;
    };

    getQuote();

    return {
      quote
    }
  },
};
</script>

Answer №3

Here is an alternative option:

{{quote.quote}}

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

Integrate a @Component from Angular 2 into the Document Object Model of another component

One of my components is called TestPage import { Component } from '@angular/core'; @Component({ selector: 'test-component', template: '<b>Content</b>', }) export class TestPage { constructor() {} } Another ...

Using a static string in Javascript yields no issues, whereas working with variables can sometimes cause problems

I've been struggling with a problem this morning and it's time to ask for help! I have a JavaScript function that takes the value entered by a user into an autocomplete box, uses AJAX to send that value to a PHP script which then queries the data ...

Create a data attribute object and assign to it the prop object received from the parent component

I am struggling with passing an object as a prop from a parent component and then utilizing it to initialize the child component with the received value. The main objective behind this is to create a dialog box that includes a child form component with mu ...

Colorful radial spinner bar

I am interested in replicating the effect seen in this video: My goal is to create a spinner with text in the center that changes color, and when the color bar reaches 100%, trigger a specific event. I believe using a plugin would make this task simpler, ...

What is the best way to add an element conditionally within a specific Vue Component scope?

I've been working on creating a Component for titles that are editable when double-clicked. The Component takes the specific h-tag and title as props, generating a regular h-tag that transforms into an input field upon double click. It's function ...

The Vue build displays a never-ending loading tab

I have a small project that includes several images and utilizes the vuetify.js library. The project functions properly when using vue serve or npm run serve. However, after running npm run build and transferring the dist folder to my Raspberry Pi Zero v1 ...

generating a new item using Mongoose searches

How can I generate an object based on queries' results? The table in the meals operates using database queries. How do I handle this if the queries are asynchronous? const getQueryResult = () => { Dinner1300.count().exec(function (err, count) ...

Error message: "Unable to access 'title' property of an undefined value" for an item with a length of 1

Despite the fact that the collection is not undefined and the `title` attribute is also not undefined, for some reason I am unable to read the `title` attribute from the `received` variable. The error indicates that it is undefined. var received = document ...

Error when spaces are present in the formatted JSON result within the link parameter of the 'load' function in JQuery

I am facing an issue with JSON and jQuery. My goal is to send a formatted JSON result within the link using the .load() function. <?php $array = array( "test1" => "Some_text_without_space", "test2" => "Some text with space" ); $json = jso ...

React and React Native not synchronizing with authentication context

It seems like there is an issue with the AuthContext not updating properly. Despite logging at various points, the user is still not being set. Here's a glimpse of the code in question: App.tsx export default function App() { const { user, setUser ...

How to access variables with dynamic names in Vue.js

I'm curious if it's possible to dynamically access variables from Vue’s data collection by specifying the variable name through another variable. For instance, consider the following example: Here are some of the variables/properties: var sit ...

Recover files from the latest commit in Git, with files having no data inside them

Hello there! I encountered an issue with Git recently. I was attempting to clone a repository in order to push my project code, but I ran into an upstream error. I tried doing a git pull without success, then attempted to revert back to my initial commit ...

Error unfound: [CLIENT_MISSING_INTENTS]: The Client requires valid intents to function properly

I've gone through multiple tutorials, but I keep encountering an uncaught TypeError. Despite following the suggested solutions, the error persists. I even tried implementing the "intent" solution, but it's prompting a change in "const client = ne ...

Exploring new classes with jQuery's .not() and :not()?

I am working on a memory game where I need to flip cards and check if two are equal. My issue is that I do not want the function to run when clicking on a card that is already flipped, or on another flipped card. I tried using jQuery's .not() and :no ...

What is the best way to create a modal popup window using Vuejs?

<style src="./LoginButton.scss" lang="scss"></style> <i18n src="./LoginButton.txt"></i18n> <script src="./LoginButton.js"></script> <template> <div class="header-login component-same ml-10"> <span v ...

Conceal the block quotes while substituting with a newline

My HTML page contains numerous comments enclosed in blockquotes. I attempted to implement a feature that allows users to hide all the comments with just one click by using the following JavaScript code: const blockQuotes = document.getElementsByTagName(& ...

What is the best way to import modules with the "@" symbol in their path when working with Node.js?

Situation In my VueJS project, I have created service modules for use with vue cli. My code makes use of the @ symbol to easily access files within the src folder: /* Inside someService.js */ import API from '@/services/APIService.js' Ch ...

Guide to eliminating hashtags from the URL within a Sencha web application

I'm currently developing a Sencha web application and I need to find a way to remove the "#" from the URL that appears after "index.html". Every time I navigate to a different screen, I notice that the URL looks like this: ...../index.html#Controller ...

Navigation bar transforms color once a specific scroll position is reached

My website features a sleek navbar fixed to the top of the window. As users scroll past a specific div, the background color of the navbar changes seamlessly. This functionality has been working flawlessly for me thus far. However, upon adding anchor link ...

Modifying subtotal values using PHP and JavaScript

I've been working on this code snippet to calculate my subtotal and determine the total payment. Can someone provide some assistance? $viewx = mysql_query("select distinct toorderid from ordercontainer where toordercategory='$ordercategory' ...