VueJS data is unresponsive upon mounting

My API is supposed to return all the currency rates. I am using a function called getRate() in the mounted section, but the value of rate['usd'] is showing as undefined. However, if I call the function again on that page, it returns the actual data. I have tried using beforeCreated and beforeMounted but they do not seem to be working. How can I make the data reactive on load, or am I doing something wrong?

<template>
  <span v-text="rate['usd']"></span>
</template>

<script>
data() {
    return {
        rate: null
    }
},
methods: {
    getRate() {
        this.$vs.loading()
        this.$http.post('wallet/rate' ,[])
        .then(response => {
            for(let key in response.data.data.data){
                this.rate[response.data.data.data[key].name] = response.data.data.data[key].value
            }
            this.$vs.loading.close()
        })
        .catch(error => {
            this.$vs.loading.close()
        })
    },
},
mounted() {
  this.getRate()
}
</script>

Answer №1

Is this functioning properly?

<template>
  <span v-text="rate.usd"></span>
</template>

<script>
data() {
    return {
        rate: null
    }
},
methods: {
    retrieveRate() {
        const rate = {}
        this.$vs.loading()
        this.$http.post('wallet/rate' ,[])
        .then(response => {
            for(let key in response.data.data.data){
                rate[response.data.data.data[key].name] = response.data.data.data[key].value
            }
            this.$vs.loading.close()
            this.rate = rate
        })
        .catch(error => {
            this.$vs.loading.close()
        })
    },
},
mounted() {
  this.retrieveRate()
}
</script>

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

Tips for creating query requests in ExpressJS

Can someone provide the correct command to write in an ExpressJS file in order to expose a single HTTP endpoint (/api/search?symbol=$symbol&period=$period)? Here is what's currently working: app.get('/api/search/', (req, res) => ...

pdfmake: Stabilized Vertical Column Dimensions

I'm working on a project where I need to add text to a column with a fixed height and width. So far, I've successfully implemented a fixed width, but I'm struggling with setting a fixed height for the column. Below is the code I've bee ...

[Vue-ChartJS] Seeking out instructions on incorporating gradients with Vuex and the reactiveProps mixin (API data) has proven challenging

Issue Link to Codepen example: https://codesandbox.io/s/vyy0z2my33 If you want to track this issue, visit: https://github.com/apertureless/vue-chartjs/issues/473 I have a project with a datepicker that triggers chart rerendering based on the selecte ...

Symfony2: Making AJAX request that unexpectedly returns complete html page

I am facing an issue with setting up a basic AJAX request in Symfony2. It appears that the controller is not receiving the request as expected. Instead of displaying '123', the AJAX response shows the HTML content of the current page (index.html. ...

I am looking to implement a permanent change using PHP Ajax

I am facing an issue with the "Add as Buddy" button on my webpage. I want it to change permanently to "Pending Request" once clicked, but it keeps reverting back to "Add as Buddy" whenever I refresh the page. Can anyone suggest a solution for this problem? ...

Ways to resolve the issue of the experimental syntax 'jsx' not being enabled

I encountered an issue while trying to implement react-fancybox and received an error. https://i.sstatic.net/vgFha.png To resolve the error, I executed the following commands: npm install --save-dev @babel/preset-react, npm install --save-dev @babel/plugi ...

Is there a way to adjust the size of the canvas element in HTML without compromising quality or resorting to zooming?

I'm currently working on a basic modeling application for the web. The main component of my app is a canvas element that I'm trying to size correctly. However, when I set the height and width using CSS, it scales the entire canvas and compromises ...

What is the best way to create pages that showcase 10 posts each?

Currently, I am facing a challenge with displaying 100 posts using the Fetch API on one single page. I am looking for a way to create separate pages where each page would showcase only 10 posts. Below is my existing JavaScript code: fetch('htt ...

Click on the navigation bar buttons to toggle between different divs and display multiple information boxes simultaneously

Here is the current code snippet... $("#contact").click(function() { if ( $( "#contact_div" ).length ) { $("#contact_div").remove(); } else { var html='<div id="contact_div" class="contact-info"><p>Contact info</p&g ...

Troubleshooting: Issue with fixed positioning in React (Next.js) causing problems with modal window functionality

I'm currently working on implementing a modal window in React, but I've encountered an issue where setting position: fixed results in the modal window's position being relative to the page rather than the browser window. For privacy reasons, ...

Error: Attempted to access undefined property 'renderMenu' in a promise without handling it

I am looking to generate a dynamic menu based on the JSON data provided below: [ { "teamId": "10000", "teamName": "Laughing Heroes", "superTeamId": "", "createTime": "2017-06-25T06:07:45.000Z", "createUserId": null }, { "team ...

Guide on replacing the "read more" text with a Vuetify icon in a Vue.js application

I am currently working on creating a photo and description display as shown below. Any ideas on how I can replace the "read more" and "read less" text with arrow icons (up and down)? <template> <v-col cols="6" > ; <row align= ...

Sorting custom strings in Javascript with special characters like dash (-) and underscore (_)

I am attempting to create a custom sorting method with the following order: special character ( - first, _ last) digit alphabets For instance, when sorting the array below var words = ['MBC-PEP-1', 'MBC-PEP01', 'MBC-PEP91&apo ...

Encountering a Syntax Error within the ng-click function of a button in AngularJS

I'm currently developing a mobile-based web application using angular js along with onsen ui. I've created a follow button on the user's profile page, allowing users to follow each other. However, when the user's profile loads and the a ...

Is Jquery Steps causing interference with the datepicker functionality?

I am currently using the jquery steps plugin for creating a wizard on my website. However, I am experiencing some issues with the functionality of the datepicker and qtip inside the steps. Even after switching the .js references, the problem still persists ...

What is the optimal approach for displaying numerous button components with varying values?

I've been developing a calculator in React and I decided to render all the buttons using the Button Panel component with what some might call a "brute force" method. return ( <> <div> <Button value="A/C" cl ...

Bower downloads the identical package but arranges it in a distinct file structure

We operate a TeamCity build server that is utilizing three different buildusers all configured similarly. We have integrated an angular/grunt project using yeoman Update 6 Noted an issue with bower https://github.com/bower/bower/issues/1709 Why does bow ...

Identifying numerous array positions within a canvas

Greetings fellow developers, Today, I have an inquiry regarding a project I am working on, akin to a Tower Defense game created using canvas. I am facing a challenge in detecting multiple circles in one coordinate. Here's an excerpt of my current dil ...

FullCalendar fails to load in Bootstrap 4 tab

My current project involves utilizing Bootstrap 4 tabs, and I encountered an issue with displaying a FullCalendar on the second tab. Specifically, I am using version 5.2.0 of the FullCalendar library. While the calendar functions correctly when placed on t ...

Prevent automatic merging of JSON data with identical IDs

My AJAX call to a PHP select request is as follows: $.ajax({ type: "POST", url: "/renforts/_getIntervenantManager", data: { IDMission : IDMission, IDManager : IDManager }, dataType : 'json' ...