Error: Attempting to display API data in a CardView using NativeScript-Vue results in a TypeError stating that property 'endpoint_link_1' is undefined

Greetings, I am a beginner in NativeScript-Vue and JavaScript. I do not have extensive experience with heavy Javascript coding. I am facing an issue with displaying data fetched from an API in CardViews. Below is the code snippet I attempted:

<template>
<Page >
<ScrollView>
    <StackLayout padding="5" for="item in cardList">
        <CardView ripple="true" elevation="10" padding="5" margin="15" height="150">
            <StackLayout>
            <Image :src="'http://URL' + item.endpoint_link_1" stretch="aspectFit" height="120"/>
            </StackLayout>
        </CardView>

        <CardView ripple="true" elevation="10" padding="5" margin="15" height="150">
            <StackLayout>
            <VideoPlayer :src="'http://URL' + item.endpoint_link_2"  controls="true" stretch="aspectFill" autoplay="false" height="120"/>
            </StackLayout>
        </CardView>
    </StackLayout>
</ScrollView>
</Page>
</template>


<script>
import * as http from "http";

export default {
    data() {
        return {
            cardList: []
        };
    },

    mounted() {
        http.getJSON("http://URL").then(result => {
                this.cardList = result.results;
                console.log(result); #This displays the results in the log confirming functionality.
                console.log(cardList); #Doesn't show anything in the log
            }, error => {
                console.log(error);
            });
    };
</script>

The structure of my JSON data is as follows:

[
    {
        "endpoint_link_1": "/default/files/image.jpg",
        "endpoint_link_2": ""
    },
    {
        "endpoint_link_1": "",
        "endpoint_link_2": "/default/files/video.mp4"
    }
]

When running the code, I encounter the error:

"TypeError: Cannot read property 'endpoint_link_1' of undefined"
. It seems like the list cardList is not properly populated or the loop for="item in cardList" might be at the incorrect location. Any assistance on this matter would be greatly appreciated.

Answer №1

try using v-for in place of the traditional for loop. Additionally, if you include console.log(this.cardList); in your script, you will be able to see the data being processed.

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 there a way to modify an existing job in Kue Node.js after it has been created?

Utilizing Kue, I am generating employment opportunities. jobs.create('myQueue', { 'title':'test', 'job_id': id ,'params': params } ) .delay(milliseconds) .removeOnComplete( true ) ...

Save an HTML5 canvas element as a picture using JavaScript and specify the file extension

Is there a way to save an Html5 canvas element as an image file using Javascript? I've tried using the CanvasToImage library, but it doesn't seem to work for this purpose. You can view my current code on JsFiddle. <div id="canvas_container" ...

Neglecting the error message for type assignment in the Typescript compiler

Presented here is a scenario I am facing: const customer = new Customer(); let customerViewModel = new CustomerLayoutViewModel(); customerViewModel = customer; Despite both Customer and CustomerLayoutViewModel being identical at the moment, there is no ...

Error: The VueComponent.onResize function encountered an uncaught TypeError because it cannot read the property 'offsetHeight' of an undefined variable

<div ref="controls" class="varie controls"> <div class="control" v-if="spin!='"> <span>Saved datee</span><span>{{ }}</span> </div> <button v-on:click="apply()">Aopply</button> </div& ...

What is the best way to position a container div over another container using Bootstrap or CSS?

https://i.sstatic.net/q1qGi.png I'm working on a Bootstrap 4 layout where container B needs to overlay part of container A. I want to achieve a design where container B appears on top of container A. Any suggestions or references on how to achieve th ...

An absence of data causes the function to malfunction

One of the components in my application is responsible for rendering select elements on the screen, allowing users to dynamically order data. The initial state is as follows: state = { sortBy: [ { author: 'asc' } ] }; In this s ...

Render the React component asynchronously, only after the data has been successfully fetched

I am looking to display a component only after the necessary data has been fetched. If I try to load the data instantly, the component gets rendered but no information is displayed. class App extends React.Component { //typical construct fetchGameD ...

What steps should be taken to prepare data for transmission to a server in a Next.js environment?

I'm in the process of creating a website that requires authentication. I am using Next (React) and typescript for web development. My objective is to make most pages ServerSideRendered or StaticHTML. However, I encountered an issue right at the begin ...

Utilizing JavaScript to display numerous variables within a text box

After creating an HTML form, I encountered an issue where only one selected item was displayed in the text field. Can anyone help me solve this problem so that multiple names can be printed in the textfield? function myFun(extras) { document.get ...

Positioning a div above a Bootstrap navbar that disappears when scrolling

I am currently using bootstrap to design a website, and I am attempting to create a div that would initially appear above the navbar on an unscrolled webpage. However, once the user starts scrolling, I want this div to disappear and have the navbar fill it ...

Get an Object Array and assign it to a state Array variable after filtering

Here is a JSON Input that I will be filtering by orderId and then setting to a state variable. [{"orderId":3,"userId":3,"firstName":"Amal","lastName":"kankanige","contactNo":"011-3456787","status":"pending","deliveryAddress":"No:24/c,Anders Road,Wijerama, ...

Maintain the current Vuetify page when there are changes made to the items

Is there a way to maintain the current page in data tables within vuetify when the items are changed? For example, if the current page is 2 and a new item is added to the items, how can we ensure that the current page remains at 2? Check out this Codepen ...

Is it possible to use JQuery to target input nodes based on their values?

How can I check if any of the file input boxes in my list have a value using just one selector statement? Is it possible to achieve this with code like the following: $('input:file[value!=null]') Or is there another way to accomplish this? ...

Utilizing a nested interface in Typescript allows for creating more complex and

My current interface is structured like this: export interface Foo { data?: Foo; bar?: boolean; } Depending on the scenario, data is used as foo.data.bar or foo.bar. However, when implementing the above interface, I encounter the error message: Prope ...

Using React's useEffect and useContext can cause issues with certain components, particularly when dealing with dynamic routes

Currently, I am developing a React blog application where posts are stored in markdown files along with metadata in Firestore. The content .md files are saved in Cloud Storage. In the App component, I utilize useEffect to retrieve the metadata for each pos ...

At which location within the script should I insert the document.title in order to update the title every xx milliseconds?

I have been working on a script that refreshes certain #id's. Additionally, I would like to update the page title, which involves some flask/jinja2. I've attempted placing document.title = {% block title %} ({{online_num}}) Online Players {% en ...

A comprehensive guide on using Lua Script in Node.js to efficiently insert multiple records into a Redis Hash

How can I efficiently insert multiple records into a Redis Hash using Lua Script in Node.js? I currently have the following code which utilizes multi and exec for inserting data. How can I modify it to use a lua script instead? return new Promise ...

Is there any method to avoid the hassle of constantly adjusting margins and paddings on my one-page website?

One issue I encountered was that the buttons weren't scrolling me to the top of the anchor, instead scrolling too far into the section due to the fixed navbar overlapping. I tried solving it with margins and paddings but believe there must be a simpl ...

Obtaining a substantial pdf file using html2pdf

While using html2pdf to generate a PDF of my website, I noticed that the downloaded PDF ends up being 14 pages long. However, after approximately 12 pages, all the colored elements seem to disappear. On mobile screens, this issue occurs even sooner, around ...

Arrange array items according to the values of two attributes

Within my array of N objects, each object is structured with the following properties: { id: 'an id', description: 'a description', isUser: true/false } My goal is to sort this array based on two criteria: Firstly, any object w ...