Retrieving Information from an Angular 2 Component

Struggling to figure this out, I am attempting to dynamically add user video data that includes a video URL. My goal is to access the data from the component so I can use it in my HTML. I've attempted the following approach.

app.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
   selector: 'app-video',
   templateUrl: './video.component.html',
   styleUrls: ['./video.component.scss'],
   providers: [
      DashboardServiceProxy
   ]
})

export class VideoComponent implements OnInit {
   videos: UserVideoDto[] = [];
   // videos: Array<UserVideoDto> = []; 

constructor(
    private _dashboardService: DashboardServiceProxy
) {
    console.log(this.videos.VideoData); //data im trying to access
  }

  ngOnInit() {
    this.getVideos();
  }

  getVideos() {
    this._dashboardService.getAllUserVideos().subscribe((result) => {
    this.videos = result;
    console.log(this.videos);
  });
  }

  }

Currently, I'm just trying to log the data but encountering an error indicating

[ts] Property 'videoData' does not exist on type 'UserVideoDto[]'

Check out the UserVideoDto[] class below

public class UserVideoDto : EntityDto
  {
    public long UserId { get; set; }

    public DateTime CreationTime { get; set; }

    public int TenantId { get; set; }

    public string VideoData { get; set; }

    public string Thumbnail { get; set; }

    public String Name { get; set; }

    public String Series { get; set; }

    public String Subname { get; set; }

    public UserVideoDto()
    { }

  }

If you have any insights or solutions, your help would be greatly appreciated.

Answer №1

Could you possibly share the UserVideoDto interface or class? The TypeScript compiler is throwing an error because there isn't a videoData field in the UserVideoDto.

If you want to test it, you can try this:

videos: Array<any> = []; // This will remove the TS error, but won't fix the underlying issue

It's important to use the correct notation like this:

videos: Array<UserVideoDto> = [];

Somewhere in your project, you should have something similar to:

interface UserVideoDto {
    ...
    public videoData: any;
    ...
}

or

class UserVideoDto {
    ...
    public videoData: any;
    ...
}

Answer №2

Your attempt to retrieve VideoData in an array is incorrect; it should be done through an instance of UserVideoDto.

The correct syntax would be this.videos[x].VideoData

Answer №3

Just as yfranz pointed out, you're attempting to access a property that doesn't exist in your UserVideoDto[] array.

Furthermore, be aware that your console.log is within the constructor, which means it won't give you the necessary data since the videos are fetched using the getVideos method inside ngOnInit. Any manipulation of 'videos' should be done after retrieving the data within the subscribe callback of _dashboardService.getAllUserVideos()

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

Updating the parameters when clicking on each pagination button: A guide

Does anyone have advice on implementing pagination? I am currently working on loading a datatable with a few records initially. Once the page is loaded, I want to be able to click on pagination buttons like next or any pagination buttons to display a new s ...

Utilize Vue 3 for setting up reactive fetching with asynchronous JSON handling

I recently upgraded to Vue 3 and I'm trying to populate a reactive object by fetching data from a JSON API. Here's how my component looks. Surprisingly, I am not encountering any errors but the expected results are not showing up either. <tem ...

Creating an array in TypeScript is a versatile and powerful feature that

While I have some familiarity with TypeScript, there is one thing that continues to intrigue me. I understand the distinction between Array<string> and string[]. I am aware that these declarations can be used interchangeably, such as: export class S ...

How to delete the final element in a stack trace string using Javascript

Currently, I am working on a Javascript logger that includes the stack trace in error messages. The implementation looks like this: function logMessage(logMessage) { let stackTrace = new Error().stack; logMessage.stackTrace = stackTrace; ... } Whi ...

What is the correct way to invoke a function from an external JavaScript file using TypeScript?

We are currently experimenting with incorporating Typescript and Webpack into our existing AngularJS project. While I have managed to generate the webpack bundle, we are facing an issue at runtime where the program is unable to locate certain functions in ...

Minimum width of Angular Material's mat-menu

I am looking to create a compact Material mat-menu using Angular 15. Below is the code I have: <mat-menu #flagMenu="matMenu"> <button mat-menu-item> <img src="assets/flags/en.png" class="flag"/> ...

Can a Vue computed property return a promise value?

I have a specific computed property in my code that triggers an API request and retrieves the required data: async ingredients() { const url = "/api/ingredients"; const request = new Request(url, { method: "GET", credentials: "same-or ...

There seems to be an issue with the visibility of the b-button within the b-table component in

I'm new to Vue Js and I'm having trouble with my b-button not displaying in my table. I can't figure out why. Below is my HTML code: <div id="listlocales"> <div class="overflow-auto"> ...

Encountering an error with my electron application built using create-react-app

While I'm working on my project, my electron window is showing this error message. TypeError: fs.existsSync is not a function getElectronPath ../node_modules/electron/index.js:7 4 | var pathFile = path.join(__dirname, 'path.txt') 5 | ...

Material Grid adamantly refused to follow the typical horizontal layout, defying its default behavior

I recently discovered Material-UI and have been trying to get two components to display side by side. However, no matter what I try, it always ends up looking like this: https://i.stack.imgur.com/w1Wyh.png Even though it is the default behavior, the Mate ...

Use Select2 for efficient selection

I am attempting to implement select2 for a multi-select dropdown list of states, but I am encountering an issue. Typically, when you type in an option, it should be highlighted so that you can press enter to select it. However, in my case, it underlines t ...

I am looking to display data in Angular based on their corresponding ids

I am facing a scenario where I have two APIs with data containing similar ids but different values. The structure of the data is as follows: nights = { yearNo: 2014, monthNo: 7, countryId: 6162, countryNameGe: "რუსეთის ...

The dropdown menu in Mantine is malfunctioning, even though I copied and pasted the exact code from

While following a tutorial, I encountered an issue with the Mantine Menu and Dropdown components. The tutorial creator did not wrap the React App inside the MantineProvider, which resulted in errors when trying to use the Dropdown component. Even after add ...

Learn how to bring a component into another component within Angular

I have developed a component named CopySchedulefromSiteComponent and now I am looking to import it into another component called SiteScheduleComponent. However, I am unsure of the correct way to do this. The CopySchedulefromSiteComponent contains one fiel ...

What could be causing the lack of data for the current user?

I have been attempting to fetch the current user session and display the data in the view, but nothing is appearing. I even checked the database and confirmed an active session with all the necessary information. I attempted logging the user out and starti ...

One issue encountered in the AngularJS library is that the default value does not function properly in the select element when the value and

In this AngularJS example, I have created a sample for the select functionality. It is working fine when I set the default value of the select to "$scope.selectedValue = "SureshRaina";". However, when I set it to "$scope.selectedValue = "Arun";", it does n ...

Guide on transferring information from a node server connected to Google Sheets to Angular 9

Encountered an issue when trying to pass data from a Node server to the front-end using Angular 9. The server side was built with Node and data was fetched from the Google Sheet API. Despite searching extensively on Google, I couldn't find a solution ...

AngularJS and IE8 don't play nice when it comes to handling dynamic content

Below is the code snippet I am working with: <blockquote class='mt20'> <p><span>&ldquo;</span><span>{{iq.quote}}</span><span>&rdquo;</span></p> <footer><cite class="d ...

AngularJS Multi-select Dropdown Filter Logic

Thank you for taking the time to review my query. Currently, I am working on an AngularJS UI-Grid project where I have incorporated a Multi-select Drop-down Menu for the "First Name" column. However, I am facing challenges in implementing the logic similar ...

After calling sequelize.addModels, a Typescript simple application abruptly halts without providing any error messages

My experience with Typescript is relatively new, and I am completely unfamiliar with Sequelize. Currently, I have only made changes to the postgres config in the .config/config file to add the dev db configuration: export const config = { "dev" ...