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

Utilizing the power of JavaScript within CSS styling

I may be new at this, so excuse the silly question, but I'm currently working on developing an app with phonegap. In order to ensure that my app looks consistent across all devices, I need to define the height of each device. I know that JavaScript ca ...

Asynchronously parsing CSV files in NodeJs using the `await` keyword within the `on('data')`

I have a specific code snippet that is designed to read lines from a .csv file one by one and then store each processed row into a database const csv = require('csv-parse') const errors = [] csv.parse(content, {}) .on('data', async ...

Tips for querying orchestrate.io

Recently, I found myself in need of a user-friendly database for a small highscore system in my game development projects using JavaScript. Through the Github student developer pack, I came across Orchestrate.io. After discovering a suitable driver module ...

Ways to reach component method via router

When making an Ajax request and displaying the data in a table component, I encounter an issue where I need to extract the clicked data for use in another Ajax request within a different component that is called through React-Router. The challenge lies in ...

What steps should I follow to ensure that the processData function waits for the data returned by the getData function in Angular?

After calling the getData function, each object is stored in an array and returned. Now I need the processData function to await these results from getData and then further process them. However, when I try to console.log(cleaningData), I don't see an ...

Using jQuery to slide elements across the page

I've been attempting to incorporate a sliding effect into my website, but I'm running into an issue where the sliding only occurs on the first click and I can't figure out why. Here is the code snippet I've been using: Html : $("#go ...

Scroll indefinitely, obliterate and regenerate elements as you scroll

I have a unique issue that I need advice on from experts in this field. On my website, I have a Tree with infinite scroll functionality. The tree's image is iterative and the number of iterations depends on the data source provided. The data source ...

Angular: Changing the class of a parent element dynamically while iterating through a list with ngFor

I have a unique challenge where I am trying to style a checkbox as a button by placing it inside its label. <label> <input type="checkbox" name="..."> </label> My goal is to toggle the parent label's class based on whether the che ...

Executing an Ajax call to trigger a NodeJS function that executes a bash script

I have created a bash script to generate a JSON file that needs to be parsed and sent to the client-side JavaScript for display. My goal is to achieve this without refreshing the page and without using jQuery. I attempted to use Axios but seem to be facing ...

I am having trouble establishing a connection between two containers on Heroku

My web application is built using Node.js and MongoDB. After containerizing it with Docker, everything worked fine locally. However, when I tried to deploy it to production, I encountered an issue where the backend could not establish a connection with the ...

After reaching the conclusion, the code restarts

Any ideas on how to reset this code every 10-20 seconds? I'm struggling to find a solution! I'm new to coding, so any assistance would be greatly appreciated. Here's the code: var items = document.getElementsByClassName('btn-primary n ...

Jest is unable to handle ESM local imports during resolution

I am encountering an issue with my Typescript project that contains two files, a.ts and b.ts. In a.ts, I have imported b.ts using the following syntax: import * from "./b.js" While this setup works smoothly with Typescript, Jest (using ts-jest) ...

Issue with conflicting namespaces warning when compiling an Angular 9 project with ng-packagr using Typescript

I'm trying to pinpoint the root cause of this problem, and I suspect it may be related to Typescript. However, it could also involve ng-packagr or Angular. This issue only arose after upgrading to Angular 9. Upon building my production environment, I ...

Is it possible to integrate the extension library of three.js (including OBJLoader, SceneUtils, etc.) with Angular 6?

Attempting to implement the below code, however, it is not functioning as expected. npm install --save three-obj-loader import * as ThreeObjLoader from 'three-obj-loader'; const OBJLoader = ThreeObjLoader(THREE); let loader = new OBJLoader(): ...

Developing an interactive website utilizing AngularJS, WCF Service, and SQL Server

Exploring the possibilities of creating a web application, I stumbled upon AngularJS. With plans to incorporate WCF Service and SQL Server into my project, I am intrigued by what these technologies can offer. I want to ensure that AngularJS aligns with my ...

javascript: verify the presence of uppercase and lowercase letters

Can the validation of at least 2 lowercase and 2 uppercase letters be implemented when checking the case? Below is the condition I am currently using. function HasMixedCase(passwd){ if(passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) return true ...

Changing properties of JavaScript arrays

Currently using Express and dealing with an array of objects that I need to adjust the created property for better presentation, utilizing the dateFormat package. The array originates from a mongo query and is stored in a variable called stories. A sample ...

Is it better to use interpolation and template expressions within *ngFor or *ngIf directives?

I am encountering challenges when attempting to interpolate variables within ngFor or ngIf in my code. My components are highly dynamic, both in terms of content and functionality, which is why I need to perform such operations. I believe it should be pos ...

Alpinejs Mega Menu Issue: Hover Functionality Glitchy

I'm working on a complex Mega Menu project that is activated upon hovering, using the powerful combination of Tailwind CSS and Alpinejs. The functionality is mostly there, but I've encountered some bugs along the way. Despite my attempts to impl ...

Attempting to update information in JSON using AJAX and PHP

I am attempting to update key values in a JSON object using PHP, AJAX, and JavaScript to display the new values. Here is an example of my JSON database that needs to be modified: "answer01count": "1", "answer02count": "2", "answer03count": " ...