The elements within the NativeScript components are failing to show the data received from the Django API

My Django API is set up to provide a list of movies titles with their corresponding IDs.

I've implemented a movie service in TypeScript that retrieves the list of movie titles and IDs using the GET operation.

In my NativeScript project, I have two files: items.component.ts

import { Component, OnInit } from "@angular/core";
import { MovieService } from "../services/movie.service";

@Component({
    selector: "ns-items",
    moduleId: module.id,
    templateUrl: "./items.component.html",
    providers: [MovieService]
})
export class ItemsComponent implements OnInit {
    items;

    constructor(private movieService: MovieService) { }

    ngOnInit(): void {
        this.movieService.getMovies().subscribe(
            movies => {
                this.items = movies;
                console.log(movies);
            },
            error => {
                console.log('error', error);
            }
        );
    }

}

and items.component.html

<ActionBar title="Movie Rater App" class="action-bar">
</ActionBar>
<StackLayout class="page">

    <ListView [items]="items" class="list-group">
        <ng-template let-item="item">
            <Label [nsRouterLink]="['/item', item.id]" [text]="item.title"
                class="list-group-item"></Label>
        </ng-template>
    </ListView>

</StackLayout>

I'm seeing a blank screen on the app in the emulator - only the action bar title is visible. No errors are shown in the logs.

I've confirmed that the API is working correctly as I can see the response in the console (from the output of `console.log(movies)`).

Any assistance would be greatly appreciated.

Django API response:

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "title": "Rocky (1976)",
            "description": "A small-time boxer gets a supremely rare chance to fight a heavy-weight champion in a bout in which he strives to go the distance for his self-respect.",
            "moviePoster": "http://127.0.0.1:8000/pic_folder/rocky-1976-poster2451370.jpg",
            "avg_rating": 5,
            "no_of_ratings": 1,
            "maxRatings": 5
        },
        {
            "id": 16,
            "title": "Rocky II",
            "description": "Rocky II",
            "moviePoster": "http://127.0.0.1:8000/pic_folder/rocky2.jpg",
            "avg_rating": 5,
            "no_of_ratings": 1,
            "maxRatings": 5
        }]
}

Answer №1

In order to properly execute the code, it is important that you assign the resulting array to items as shown below:

this.movieService.getMovies().subscribe(
        movies => {
            this.items = movies.results;
            console.log(movies);
        },
        error => {
            console.log('error', error);
        }
    );

Furthermore, consider removing the StackLayout wrapping the ListView if it's unnecessary.

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

Guide to creating a setup.py file with a Git repository as a required dependency

Currently, I am in the process of creating the setup.py file for my package. One of the requirements for my package is to specify a dependency on another Git repository. This is the current progress I have made: from setuptools import setup, find_packages ...

What is the process of transforming a basic JavaScript function into a TypeScript function?

As a Java developer diving into TypeScript for frontend development, I've encountered a simple JavaScript code snippet that I'd like to convert to TypeScript. The original JavaScript code is: let numbers = [123, 234, 345, 456, 567]; let names = ...

Angular - Conceal Protected Links on the Template

In my AuthGuard, I have implemented CanActivate which returns either true or false based on custom logic: import { Injectable } from '@angular/core'; import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular ...

Flow - secure actions to ensure type safety

Currently implementing flow and looking to enhance the type safety of my reducers. I stumbled upon a helpful comment proposing a solution that seems compatible with my existing codebase: https://github.com/reduxjs/redux/issues/992#issuecomment-191152574 I ...

Exploring how process.argv in NodeJS can be utilized within JavaScript code compiled by

I'm having trouble compiling a basic TypeScript file using webpack (with 'awesome-typescript-loader') that needs to access command line arguments. It seems like the compiled JavaScript is causing a problem by overriding the Node 'proce ...

Implementing a NestJs application on a microcomputer like a Raspberry Pi or equivalent device

I'm facing a challenge in trying to find a solution for what seems like a simple task. I am aware that using the Nest CLI, I can utilize the command "nest build" to generate a dist folder containing the production files of my project. However, when I ...

What is the proper way to arrange dates within strings in Angular?

I'm currently facing an issue with sorting two strings. The strings in question are: "2022 | Dec (V2 2022)" "2022 | Jul (V1 2022)" Although I am attempting to sort them using localeCompare, it is not yielding the correct result. T ...

Tailored component properties for React applications

I am currently working on configuring discriminative component props. Check out my code snippet below: import React, { ReactNode } from 'react' type SelectionModalProps<T> = ( | { multiSelect: true onSubmit: (data: T[]) => ...

Axios is returning a 401 (Unauthorized) error when attempting to retrieve user data from a React frontend and pass it to DRF Social Oauth2. Strangely, the same process

Here are the contents of two files, LoginScreen.JS and login.JS. The LoginScreen.JS file contains a submit handler that submits user input, while we import the axios instance from login.JS. In addition, a working example from PostMan has been provided. lo ...

How can I avoid hardcoding values in my post request if I want to make it more dynamic?

At the moment, I have been manually inputting values into the payload and headers for my post request, which has been working fine. However, I now need a way to dynamically accept these values during the post request. Does anyone have any suggestions on h ...

Angular sub-route is failing to activate

My current setup involves Angular routing along with the use of ngx-translate-router, and I've encountered an unusual issue with child routes. It's unclear whether this problem is connected to the translated router module I'm utilizing, but ...

What is the best way to update the displayed data when using Mobx with an observable array?

Is there a way to re-render observable array data in Mobx? I have used the observer decorator in this class. interface IQuiz { quizProg: TypeQuizProg; qidx: number; state: IStateCtx; actions: IActionsCtx; } @observer class Comp extends Rea ...

I am encountering issues with running `python manage.py syncdb` on Windows 7 with MySQL

Utilizing Python Django along with MySQL, I have diligently followed the guidelines provided in the tutorial. However, despite my efforts, I am encountering an error. Here is a summary of the issue: DATABASES = { 'default': { 'E ...

Adjust website content depending on user's authentication status

My goal is to display a logout button when the user is logged in and a login button if they are not. I am using JSON tokens to determine if a user is logged in or not, by checking if the token is null. However, this approach does not seem to be working. Ca ...

*ngFor is not rendering

I understand that this question is frequently asked, but the usual solutions don't seem to be effective in this case. Here is my code snippet: export class CourseReviewFormComponent implements OnInit { form: FormGroup questionnaire: string[] = [] ...

Learn how to import from a .storybook.ts file in Vue with TypeScript and Storybook, including how to manage Webpack configurations

I'm currently utilizing Vue with TypeScript in Storybook. Unfortunately, there are no official TypeScript configurations available for using Vue with Storybook. How can I set up Webpack so that I am able to import from another .storybook.ts file with ...

Encountering an Object Type Unknown error while working with Typescript and React

I am currently working on building a chatbox in React using TypeScript and Firebase. Below is the code for my Room and Message components: function ChatRoom() { const messagesRef = firestore.collection('messages'); const query = messagesRef.o ...

Exploring Angular5 Navigation through Routing

I have been working with Angular routing and I believe that I may not be using it correctly. While it is functional, it seems to be causing issues with the HTML navbars - specifically the Info and Skills tabs. When clicking on Skills, a component popup s ...

What is the best way to add a 'Drawer' component into the 'AppBar' using React MUI within the Next.js framework?

My goal is to create an App bar with a 'hamburger' icon on the left that, when clicked, will display a Sidenav (drawer). I am working with React Material and Next.js (App router) and need to have the app bar and drawer as separate components, hea ...

Unable to instantiate a Vue reference with a potential null type

When working in Vue v3, I encountered a situation where I needed to create a ref with a type that could possibly be null. This specific scenario in my app involves data that starts off as null and then gets populated after loading completes. Just to illus ...