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

Converting an array of 8-bit unsigned integers into a string without the

Currently, I am working on extracting JSON data from an HTML document stored in a Uint8 array. The process involves converting the array to text and then isolating the JSON within a script tag. To achieve this, I initially converted the array into text fo ...

The command "json_encode($array)" does not seem to be encoding the complete array

I ran a test on the following: <? echo json_encode($array) ?> result: ["PG","Kevin Sad","8000","12"] Upon placing it within a form option value to be selected by my script function: <option value=<? echo json_encode($array) ?> > op ...

Error with Bootstrap 4 tabs and JavaScript AJAX implementation

I have implemented Bootstrap 4 tabs to showcase content fetched through an AJAX call. However, I encountered an issue upon completion of the call. The error message displayed is: Uncaught TypeError: $(...).tab is not a function The tabs are initially hi ...

What is the process for removing a key from an object and replacing it with its corresponding value?

My JSON sample looks like this: var obj={ "results":{ "grade":"A", "marks":12 }, "data":{ "name":"sam", "gender":"male", "age":10 } }; I am trying to transform the above JSON to: var obj={ "res ...

Is it possible to send information via the URL while using jQuery Mobile?

My mobile application utilizes an in-app browser to send information, such as deviceID, to my server via a URL. For instance, the browser opens a web-page (jquery Mobile): www.myserver.com/doWork.html#deviceID Within the doWork.html file on the server si ...

Display a collection of objects using Jade / Pug syntax

Struggling to find resources on Pug / Jade templating, turning to this platform for help. I've been diving into documentation on iterations along with consulting this helpful link. Working with Node.js, Express, and pug for a school project - a mock ...

Warning: Attention Required for Published NPM Package Fix

After successfully publishing my first package on npm, I encountered a warning when I tried to import it in Codesandbox. There was an error converting '/node_modules/protected-react-routes-generators/src/index.js' from esmodule to commonjs: Canno ...

Text changing in Vuejs div

My Vue component contains some data and is connected to an element in the script tag of the component. However, when another component pushes new data to this one, the div does not dynamically update. Although the data changes (confirmed by console.log), t ...

Tips for updating the selected value in Vue dynamically

I am currently working on a feature where the months change dynamically based on the selection made using Vue. You can view the code snippet in the link provided below. <div id="app"> <span class="selectboxWrap"> < ...

Navigating through various arrays within objects on a JSON API with JavaScript: A walkthrough

I am currently working on fetching and displaying data from an API. The specific data I am interested in is located within the 'equipments' array. You can see an example of this array in the image linked below: https://i.sstatic.net/QeBhc.jpg M ...

Removing fragment identifier from the URL

If you go to stackoverflow.com/#_=_, the expression window.location.hash will display #_=_. All good so far. But when you run window.location.hash = '' to get rid of the hash, the URL changes to stackoverflow.com/#. (Note the unexpected # at the ...

My attempt to use the Redux method for displaying data in my testing environment has not yielded

I've been attempting to showcase the data in a Redux-friendly manner. When the advanced sports search button is clicked, a drawer opens up that should display historical data when the search attributes are selected. For this purpose, I implemented the ...

ERROR: The variable countryCallingCode has not been defined

I encountered an error when attempting to assign a value to my property countryCallingCode, which does not exist in the first option. this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode The error message I received was: ERROR ...

Using dynamic classes within a v-for loop

Edited How can I dynamically assign classes to multiple elements within a v-for loop? Since I cannot utilize a computed property due to the presence of v-for, I attempted using a method by passing the index of the element, but that approach did not yield t ...

Passing data between API tests in JavaScript

I'm encountering an issue where I need to create e2e api tests. The goal of the first test is to obtain a token for an unauthorized user, use that token in the method header for the second test to return a token for an authorized user, and then contin ...

Every time I refresh the app, I am consistently redirected back to the home route "//"

I'm facing an issue where, after logging in successfully, I get redirected to the homepage. However, when I refresh the page from any route other than the homepage, such as "/products", I always end up getting redirected back to "/". This is what my ...

Managing Numerous Dropdown Menus: Techniques and Tips

I'm currently working with MUI to design a navigation menu that contains dropdowns within certain links. However, I've encountered an issue where clicking on any button opens the same dropdown menu. Below is my code snippet: function Header() { ...

The Jquery AjaxStop event fails to trigger, but AjaxStart works perfectly

While I was working with APIs, I encountered a roadblock. I have a Jquery function that retrieves data from an API. To prevent a "reposition" effect, I need the function to wait until all calls are made before triggering another function to place all the ...

Utilizing logic classes in conjunction with styled components

I am seeking a way to utilize this logic in order to assign the appropriate class to an element: <ul onClick={handleClick} className={click ? 'dropdown-menu clicked' : 'dropdown-menu'}> However, as I am employing styled component ...

What is the best way to stack a canvas box on top of another canvas box?

My goal is to have two canvas squares stacked on top of each other. While I am familiar with drawing on canvas, I need assistance in placing one canvas on top of another. Despite my attempts at setting the position, I have not been successful. <canvas ...