I'm puzzled as to why a div tag suddenly becomes invisible when I attempt to link it with a v-for directive in my code

I am facing an issue with the div tag assigned to the log class. My objective is to populate the text using data retrieved from the API response. However, when I attempt to include the v-for directive, the entire div mysteriously disappears from the browser without any console errors being displayed.

<template>
  <div>
    <div class="log" v-for="info in infos" v-bind:key="info" v-text="info.login">
      Username
    </div>
  </div>
</template>

<script>
  export default {
    name: "HelloWorld",
    data() {
      return {
        name: '',
        infos: null,
      }
    },
    methods: {
      hello() {
        let user = document.querySelector(".search").value;
        let fullname = user.split(" ").join("");
        let msg = "No Username Found";

        const axios = require("axios");
        axios.get("https://api.github.com/users/" + fullname)
          .then(response => (this.infos = response.data))
          .catch(error => alert(error + " " + msg))
      },
      reset() {
        this.name = "";
      }
    }
  };
</script>

Answer №1

The data for infos is currently null, so the div will not be displayed on the page. However, if you utilize the created function that I have included below and call the hello() method, it will populate the infos data and the div will then be shown.

<script>
  export default {
    name: "HelloWorld",
    data() {
      return {
        name: '',
        infos: null,
      }
    },
    created() {
      hello();
    },
    methods: {
      hello() {
        let user = document.querySelector(".search").value;
        let fullname = user.split(" ").join("");
        let msg = "No Username Found";

        const axios = require("axios");
        axios.get("https://api.github.com/users/" + fullname)
          .then(response => (this.infos = response.data))
          .catch(error => alert(error + " " + msg))
      },
      reset() {
        this.name = "";
      }
    }
  };
</script>

Answer №2

consider utilizing the computed method

<template>
  <div>
    <div class="log" v-for="information in infosComputed" v-bind:key="information" v-text="information.login">
      Username
    </div>
  </div>
</template>
...
computed {

  infosComputed: function() {
     return this.information;
   }

}

the information provided may not be dynamic

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

Is it necessary to ensure the complete loading of the API is complete before proceeding to the next function when using async/await?

I have some experience with Javascript and a little bit of VueJs knowledge. Currently, I am working with an array named tickets and receiving data from an API which returns two different data sets - tickets and user profiles. The ticket objects contain us ...

Tips for securing apiKey and apiPass in Shopify using VueJs

Implementing Shopify's admin API in VueJs to handle some Api calls. However, I am concerned about the security of sending the Key and Password as it appears they are exposed on my site. How can I ensure their protection? Or perhaps I have set it up in ...

What is the best way to set a fixed width for my HTML elements?

I am facing an issue with my user registration form where error messages are causing all elements to become wider when they fail validation. I need help in preventing this widening effect. How can I achieve that? The expansion seems to be triggered by the ...

Prevent identical values from being added repeatedly to a table while updating in real-time

Currently, I am facing an issue while running through a loop to extract values from an array. When I add a second value, it duplicates both the new and previous values in the table. For example, adding a row with value 488 works fine: <tr class="exe"& ...

Encountering an issue with receiving "undefined" values while utilizing WordPress post metadata in AngularJS

Utilizing the Wordpress REST API v2 to fetch data from my functional Wordpress website to an AngularJS application. Everything is functioning properly, however when I attempt to access post meta such as "_ait-item_item-data", it returns an error stating "u ...

Update the numerical data within a td element using jQuery

Is there a way to use jquery to increase numerical values in a <td> element? I've attempted the following method without success. My goal is to update the value of the td cell by clicking a button with the ID "#increaseNum". Here is the HTML st ...

Sending data using Ajax to the server-side code in ASP.NET

Struggling to successfully pass a series of values through Ajax to a code-behind method in order to insert the data into a database table. However, encountering issues where string variables are being received as empty strings and int variables as 0. The ...

How should you correctly display the outcome of a mathematical function on a data property in a v-for loop in VueJS?

Recently, I've been developing a dice roller using Vue for a game project. The approach involves looping through different types of dice with v-for to create buttons and display the result in an associated div element. However, despite correct console ...

What steps should be taken to retrieve the contents of a file that has been chosen using the browse

You have successfully implemented a browse button that allows the user to navigate the directory and choose a file. The path and file name are then displayed in the text element of the Browse button complex. Now, the question arises - how can I extract dat ...

Transforming seconds into years, months, weeks, days, hours, minutes, and seconds

Can anyone help me modify Andris’ solution from this post: Convert seconds to days, hours, minutes and seconds to also include years, months, and weeks? I am currently running this code: getDateStrings() { console.log(req_creation_date); const toda ...

Guide on Implementing javascript pagination in Vue.js component

I've been facing a challenge while trying to implement pagination code in my Vue component. I attempted to add the code within the mounted hook to trigger the function, but unfortunately, it did not work as intended. My goal is to load the code after ...

Chrome and Firefox provide excellent compatibility for running JavaScript, whereas Safari may encounter some issues. Opera's performance with JavaScript can be quirky

Disclaimer: I'm new to web design and development. I have encountered an issue with posting information from a form built on CodeIgniter using jQuery. The form posts successfully in Chrome and Firefox, with the current page automatically reloading. H ...

Find all strings in the array that do not begin with a letter from the alphabet

When a specific button is clicked, I need to filter the example array provided in the snippet below. The expected output should only include elements that do not start with an alphabet. I attempted: const example = ["test", 'xyz', '1 test& ...

Is there a solution to rectify the error related to POST net::ERR_CONNECTION_REFUSED?

Every time I try to post via ajax, an error keeps popping up. Here are the snippets of my code: let xhr = new XMLHttpRequest() let data ={ "name": "test", "phone": "12345678", "email": &qu ...

Encountering a syntax error while attempting to import modules from amCharts

Recently, I have been attempting to incorporate amcharts into my project using npm. After running the command npm install@amcharts/amcharts4, I noticed that all the necessary modules were now present in my node_modules folder and package.json file. Specifi ...

What is the best way to notify my form that a payment has been successfully processed?

I'm working on a form that has multiple fields, including a PayPal digital goods button. Clicking on this button takes the user out of the website's workflow and into a pop-up window for payment processing. Once the payment is completed, the retu ...

Leverage the power of Angular and Node.js to dynamically generate and configure a new subdomain on an

Currently, I am tackling a project that involves generating sub domains dynamically based on the prefixes entered by users on my website. While everything is functioning properly on my localhost using diet.js and express-subdomain, errors are being encount ...

Having trouble with sending a post request through ajax

Whenever I try to initiate an Ajax request upon clicking a button, the request never seems to get executed. Below is the snippet of my HTML code : <!DOCTYPE html> <html lang="en"> <head> <title>User Form</title> ...

Unable to insert rows into an array using sqlite3 in Node.js

I have a snippet of Node.js code that queries a SQLite Database and prints each row individually. The original code is as follows: var sqlite3=require('sqlite3').verbose(); var db=new sqlite3.Database('./database.db',(err)=>{ i ...

Retrieve the difference in time from the current moment using moment.js

I am trying to implement a functionality where I can track when my data was last updated. - After hitting the 'Update' button, it should display 'Update now' - If it has been n minutes since the update, it should show 'n mins ago& ...