Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,155,306 members, 7,826,213 topics. Date: Monday, 13 May 2024 at 11:16 AM

How Can I Loop This Data In Vuejs - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / How Can I Loop This Data In Vuejs (359 Views)

Help Review This Data Analytics Project / I Need Help With This Data Analysis, It Is Urgent. / Vuejs Confirm Dialog Plugin (2) (3) (4)

(1) (Reply) (Go Down)

How Can I Loop This Data In Vuejs by Tunechi5(m): 7:51am On Apr 04, 2023
this is my script


<script>
import axios from 'axios';

export default {
data() {
return {
status: false,
responseObject: null,
response:"",
}
},
async mounted() {
try {
let response = await axios.get("http://localhost:4000/api/blogs");
this.responseObject = response.data;
console.log(response);
} catch (e) {

}
},

}
</script>

html template

<template>
<div>
<div >
<li v-for="service in responseObject">
{{ service }}
</li>
</div>

</div>
</template>
{{service}} returns this json

{
resource: "services",
status: "success",
data: [
{
service_id: "0001",
service_name: "Buy Airtime",
service_type: "airtime",
},
{
service_id: "0002",
service_name: "Buy Data",
service_type: "Data",
},
],
},
if i do {{service.service_name}} it doesn't input anything
Re: How Can I Loop This Data In Vuejs by niel63(m): 8:35am On Apr 04, 2023
If I am correct, your responseObject is an array containing the following:

resources
status
data

data which is an array is what you're meant loop through.

Now, what you can do is, destructure your responseObject like this.
const {resources, status, data} = responseObject.value[0]

Remember your responseObject is an array.

Then kiss your data anyhow you like.

<li v-for="service in data"> {{service}} </li>


I no test am ooo! Hope it works.

Why do you like the options API? Have you tried the composite API? Your logic will look simpler using the <script setup>
Re: How Can I Loop This Data In Vuejs by davien(m): 8:37am On Apr 04, 2023
Since axios returns a promise you can use .then() to further turn the response.data to just data then loop through the values you want with .map() and arrange how you want.

Note: If the code you listed returns the json response to the console. You'll still need to JSON.parse(response.data) though I'm not that familiar with axios I typically use fetch API.
Re: How Can I Loop This Data In Vuejs by Tunechi5(m): 8:40am On Apr 04, 2023
niel63:
If I am correct, your responseObject is an array containing the following:

resources
status
data

data which is an array is what you're meant loop through.

Now, what you can do is, destructure your responseObject like this.
const {resources, status, data} = responseObject.value[0]

Remember your responseObject is an array.

Then kiss your data anyhow you like.

<li v-for="service in data"> {{service}} </li>


I no test am ooo! Hope it works.

Why do you like the options API? Have you tried the composite API? Your logic will look simpler using the <script setup>

i get ReferenceError: responseObject is not defined
at VueComponent.mounted
Re: How Can I Loop This Data In Vuejs by niel63(m): 8:40am On Apr 04, 2023
davien:
Since axios returns a promise you can use .then() to further turn the response.data to just data then loop through the values you want with .map() and arrange how you want.

Note: If the code you listed returns the json response to the console. You'll still need to JSON.parse(response.data) though I'm not that familiar with axios I typically use fetch API.

Async/Await, Try/Catch works better for me whethere using axios or fetch... besides, that's not where his problem is.
Re: How Can I Loop This Data In Vuejs by niel63(m): 8:42am On Apr 04, 2023
Tunechi5:


i get ReferenceError: responseObject is not defined
at VueComponent.mounted

still await the response.data ure applying to the responseObject.
Re: How Can I Loop This Data In Vuejs by davien(m): 8:43am On Apr 04, 2023
niel63:


Async/Await, Try/Catch works better for me whethere using axios or fetch... besides, that's not where his problem is.
Alright, I'm new to asynchronous requests though I know you can still use try/catch for fetch responses also. I think it's responseObject[0].value you meant to tell him by the way.
Re: How Can I Loop This Data In Vuejs by niel63(m): 8:44am On Apr 04, 2023
This is one thing with vue. E sweet ooo but it's not as straightforward as react. I like it still sha.
Re: How Can I Loop This Data In Vuejs by Tunechi5(m): 8:47am On Apr 04, 2023
niel63:


still await the response.data ure applying to the responseObject.

dont get you this my full code


the second image is me hard coding the details it returs the json in browser

Re: How Can I Loop This Data In Vuejs by niel63(m): 9:29am On Apr 04, 2023
try

const {resources, status, data} = await this.responseObject.value[0];
You forgot to put the "this".

I dey road, I for help you check am well.

Console log the destructured data not the response.
Re: How Can I Loop This Data In Vuejs by Tunechi5(m): 10:11am On Apr 04, 2023
niel63:
try

const {resources, status, data} = await this.responseObject.value[0];
You forgot to put the "this".

I dey road, I for help you check am well.

Console log the destructured data not the response.
not working cannot read value of [0]
Re: How Can I Loop This Data In Vuejs by arejibadz(m): 12:14pm On Apr 04, 2023
your code looks fine what you missing is
response.data.responseObject
so just modify your code to this
async mounted() {
try {
let response = await axios.get("http://localhost:4000/api/blogs");
this.responseObject = response.data.responseObject;
} catch (e) {
console.log(e);
}
}

then put this in your template

<li
v-for="service in responseObject.data"
:key="service.service_id"
>
{{ service.service_name }} - {{ service.service_id }}
</li>

this should work fine
Re: How Can I Loop This Data In Vuejs by Tunechi5(m): 12:16pm On Apr 04, 2023
thanks @arejibadz your a life saver......been on this since yesternight..... thanks again
Re: How Can I Loop This Data In Vuejs by arejibadz(m): 12:19pm On Apr 04, 2023
Tunechi5:
thanks @arejibadz your a life saver......been on this since yesternight..... thanks again
cheers happy learning

1 Like

(1) (Reply)

Mark Zuckerberg: Threads Users Down By More Than A Half / Big Data, AI, Iot & Cloud Computing: Futuristic Approach? / Web Hooks And Nigeria Outdated Technology

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.