The program encountered an issue: TypeError - It is unable to access the property 'files' of an undefined value

Incorporating both ng2-file-upload and angular2-img-cropper into my Angular(2+) project has brought about a unique challenge.

My goal is to utilize ng2-file-upload's Dropzone feature to load an image into angular2-img-cropper's cropping tool. While I am able to successfully load the selected image into the crop tool by changing the Input File (HTMLElement), I encounter issues when attempting to do so with the drag-drop zone.

Here is how I have managed to achieve it using the input file:

HTML:

<input type="file" id="file-name"
                    ng2FileSelect
                    [uploader]="uploader"
                    (change)="fileChangeListener($event, cropper)"/>

TS:

fileChangeListener($event : any, cropperComp: ImageCropperComponent) {

    this.cropper = cropperComp;

    let image = new Image();
    var file:File = $event.target.files[0];
    var myReader:FileReader = new FileReader();
    var that = this;

    myReader.onloadend = function (loadEvent: any) {
        image.src = loadEvent.target.result;
        that.cropper.setImage(image);
    };
    myReader.readAsDataURL(file);

}

This approach works without any issues.

However, when attempting to implement the Dropzone functionality, I encountered the following error:

ERROR TypeError: Cannot read property 'files' of undefined [...] at ProfileComponent.fileChangeListener

HTML (dropzone):

<div ng2FileDrop
                    [ngClass]="{'nv-file-over': hasBaseDropZoneOver}"
                    (fileOver)="fileOverBase($event)"
                    [uploader]="uploader"
                    class="well my-drop-zone"
                    (onFileDrop)="fileChangeListener($event, cropper)"
                    >
                    Base drop zone
                </div>

Answer №1

let document:File = $event.target.files[0];

It should look like this:

let document:File = $event.files[0];

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

Clicking on a column or x-axis category in Highcharts will automatically include the job number in the page URL

Check out the code I've put together: http://jsfiddle.net/a9QwS/ I'm looking to add functionality where clicking on a column or x-axis label will append its data to the URL. For example, in PHP, I can do something like this: echo " <td sty ...

I am looking to modify the highlighted table cell whenever the value within it changes

I am currently working on a small project related to the Stock Market. In this project, I need to dynamically change the style of the td element based on data fluctuations - green highlight for an increase and red highlight for a decrease. In the provid ...

Making a JSON request using YQL and MooTools

As a novice in JavaScript and web technologies, I am currently working with JSON files from another domain. I am attempting to make a cross-domain request by using YQL as a proxy. Although I acknowledge that my code may not be the most elegant at this poin ...

In the world of Typescript, object-based type inference reigns

I'm grappling with TypeScript to correctly deduce typing in the given code snippet: type Customer = { name: string } type Item = { price: number } const customerConfig = { action: () => [{name: 'Alice'}] as Customer[], } const item ...

Creating a subclass in JavaScript ES6+ that directly returns an object within its constructor

I am facing an issue with extending a Javascript ES6+ class that returns an object from the constructor. Consider the following scenario: class Base { constructor() { return {name: "base"} } } class Extension extends Base { constructo ...

Connecting the dots between the price and the menu item

I am currently working on creating a menu with a list of dishes, and I need to include dots to separate the price from the description. I want it to look something like this: https://i.sstatic.net/h7PO4.png If the description is too long, it should look l ...

Using Bootstrap's nav-pills inside a table

Is there a way to incorporate Bootstrap Nav Pills into a table, with the "tab buttons" located in the last row of the table? I've attempted it but it doesn't seem to be functioning correctly. Check out this link for reference <table clas ...

Is there a simpler and more refined approach for handling Observables within RxJS pipelines?

Picture this: I have an observable that gives me chocolate cookies, but I only want to eat the ones without white chocolate. Since I am blind, I need to send them to a service to determine if they are white or not. However, I don't receive the answer ...

Merging arrays with the power of ES6 spread operator in Typescript

My goal is to merge two arrays into one using the spread object method as shown in the code snippet below: const queryVariable = { ...this.state, filters: [...Object.keys(extraFilters || {}), ...this.state.filters], } The this.state.filte ...

What is the best way to organize an array of objects into distinct arrays based on their values?

For example: array = [{name:'Anna'}, {name:'Bob'}, {name:'Joe'}, {name:'Anna'}] I am looking to group this array based on the name attribute in order to create separate arrays like: array 1 = [{name:'Anna&ap ...

What is the best way to utilize functions from different JavaScript files?

I'm currently handling server-side javascript and I've got confidential data that needs to remain secure, stored in a private directory. If I choose to enclose this data within a function, how can I go about invoking that function from a separate ...

Using ThreeJS to load and display several meshes from a .json 3D file

I downloaded a .json file from an online 3D editor and now I'm facing an issue while trying to load and create 20 instances of it, similar to this example. Sadly, my code seems to be flawed as all 20 instances are behaving as if they are the same obje ...

Merging Two JSON Objects into a Single Object Using Angular 4-6

Two JSONs are available: The first one (with a length of 200): {date_end: "2099-12-31", id: "2341"} {date_end: "2099-12-31" id: "2342"} ... The second one (length 200): {type: "free", discount:"none", warranty: "yes"} {type: "free", discount:"none", ...

Navigate the user to various pages in a Node.js application based on login status

Looking for guidance on a login system within my application? I need to redirect users based on their roles. Below is the code for the login controller, along with node.js snippets and the login HTML page. Any help would be appreciated. Login.html - < ...

NVDA now prioritizes reading only the most recently loaded content in a div, disregarding the rest

When loading 3 different div elements dynamically based on search criteria, I have added attributes "role='alert'", "tabindex='0'", and "aria-live='polite'" to each div. The DOM loads all 3 div el ...

Request for removal in Express.js

Currently in the process of developing a MERN-stack app, but encountering issues with the delete request function. Here is the relevant code snippet: Upon attempting to send a delete request via Postman, an error message is displayed. I have researched ...

What is the best way to toggle between rendering two components or updating two outlets based on a route in React Router?

There is a React application I am working on that utilizes React-Router. My layout component has the following structure: import React from 'react'; import Header from './components/Header/Header'; import Footer from './components/ ...

Updating binary files using Node.js

I am currently working on handling binary files in Node.js. My goal is to receive a binary file from the client, open it, convert it to hexadecimal, make some modifications, and then send back the updated binary file to the client. app.use('/read-bin ...

Display element on the webpage upon clicking an event

I have a simple project in mind where I want to create an application that consists of a textArea component and a button. When the button is clicked, a function will take the user input and generate an object with each word and its frequency count. The ma ...

Switch between dark and light themes on the Tradingview widget

I have integrated a TradingView widget into my website and I am working on adding a dark/light mode switch to the site. I would like the widget color to change along with the background in the switch. See Widget Screenshot TradingView Widget - Widget sour ...