Vue 3 Composable console error: Unable to access properties of undefined (specifically 'isError') due to TypeError

I recently developed a Vue 3 / TypeScript Composable for uploading images to Firebase storage. The code snippet below illustrates the structure of the ImageUpload interface:

interface ImageUpload {
  uploadTask?: UploadTask;
  downloadURL?: string;
  progress?: number;
  error?: Error;
  isCanceled: boolean;
  isRunning: boolean;
  isPaused: boolean;
  isSuccess: boolean;
  isError: boolean;
}

export const useImageUpload = (file: File) => {
  // Implementation details here...
};

When implementing this Composable in a Vue component, the following code snippet is used:

<template>
  <!-- Code snippet goes here -->
</template>

<script setup lang="ts">
// Script setup details
</script>

An issue arises when running the application, as console errors are displayed including "TypeError: Cannot read properties of undefined". This error indicates that certain properties such as 'isError' are not defined.

To resolve this issue and avoid declaring variables at the top level, alternative solutions need to be explored.

Answer №1

The solution was to initialize the mandatory imageUpload properties.

Remove the following line:

let imageUpload: Ref<ImageUpload>;

Instead, add this code snippet:

let imageUpload = ref<ImageUpload>({
  isCanceled: false,
  isRunning: false,
  isPaused: false,
  isSuccess: false,
  isError: false,
});

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

Issues arise with the functionality of webcams in conjunction with JavaScript on select devices

I am currently working on developing an Electron application using Vue.js that incorporates a webcam feature. Interestingly, the webcam functions perfectly within the Electron application on one computer but only displays a black screen on another computer ...

Can you please tell me the event that is triggered when an option is unselected in Vue.Drag

I am trying to figure out the event for selecting an item since I already know about the "choose" event. However, I am uncertain about what to use for un-selecting an item. In my particular case, I am unable to utilize the @end event. <draggable :list= ...

Tips for testing "defineAsyncComponent" in Vue 3:

Suppose I need to unit test this utility function. I am utilizing Vue 3, however, this code resides in a "regular" JavaScript file rather than an SFC. How can I go about doing that? function getDynamicComponent() { if (...) { return defineAsyncComp ...

Signaling events from components to router-view – a red flag in the code base

As I delve into Vue Router, I'm detecting a peculiar odor. Before proceeding further, I feel the need to seek advice from the community to ensure that I am on the right track. Prior to Vue Router, I had a parent component receiving an event from its ...

Reverting Vue Draggable Components to Their Original Position Upon Dropping Them

In my Vue 3 project, I'm implementing vuedraggable to enable element reordering within an expansion panel. However, I've encountered an issue where the dragged elements revert to their original positions upon release. This behavior suggests that ...

Tips for personalizing a long press instruction with rxjs in Vue or any other frameworks

I'm currently working on creating a custom longpress or longtouch directive for use in my Vue project. The issue I'm facing is that the code I've written triggers console.log('startObser') after every touch event. Even when there& ...

The Angular AJAX call was unsuccessful due to the Content-Type request header field being forbidden by the Access-Control-Allow-Headers in the preflight response

Here is the code I am using to send a post request from Angular 6 to my web service. const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); const headeroptions = { headers: headers }; return this.http.post(this. ...

Is there a way to ensure an ajax call finishes executing without relying on 'async: false' or callbacks?

In my view, I have implemented a TypeScript code defining a KnockoutJS binding handler for a clickable element as shown below: module MyModule { export interface ICopyButtonParams { dataUrl: string; } ko.bindingHandlers.copyButton = { ...

Encountering a VUE error during unit testing operations

Upon adding a test unit and running it using npm run unit, I encountered an error causing the test to fail. However, everything appears to be working fine when I run npm run dev. It seems that when running with npm run unit, the sass-loader is unable to re ...

Simulating TypeDI service behavior in Jest

My current setup includes Node with TypeScript, TypeDI and Jest. I've been working on creating services that have dependencies on each other. For example: @Service() export class MainService{ constructor(private secondService: SecondService){} public ...

Reserve a spot for a credit card in 4-digit format with VueJS

When I enter a credit card number, I want to automatically insert a space after every 4 digits. For example: 0000 0000 0000 0000 I am currently using vue js and have come across examples using jquery, but I prefer not to use jquery. Any help would be appr ...

Migration of Angular dynamic forms project - The error "input" does not have an initializer or a constructor, and another issue with Type T | undefined

Angular dynamic forms project migration - encountering Type T | undefined error In my quest to find a sample project demonstrating the creation of Angular forms using JSON datasets, I stumbled upon this repository: https://github.com/dkreider/advanced-dyn ...

Every time I attempt to destructure the state object in react typescript, I encounter the error message stating 'Object is possibly undefined'

Whenever I attempt to destructure my state object in react typescript, I encounter an error stating Object is possibly 'undefined'. When I try using optional chaining, a different error pops up saying const newUser: NewUser | undefined Argument o ...

Having trouble resolving 'primeng/components/utils/ObjectUtils'?

I recently upgraded my project from Angular 4 to Angular 6 and everything was running smoothly on localhost. However, during the AOT-build process, I encountered the following error: ERROR in ./aot/app/home/accountant/customercost-form.component.ngfactory. ...

Testing a feature in Angular that modifies a variable

Trying to test a function that updates a Boolean variable has been causing some confusion for me. The strange thing is, even though the function seems to be called successfully when using the toHaveBeenCalled method, the variable itself never actually gets ...

What is the best way to streamline the code by mounting a Vue component using typescript in the beforeEach hook?

My code needs to be refactored to remove redundancy. describe("Stored id", () => { it("ID empty", () => { // Performing actions const wrapper = mount(SigninPage, options); const vm = wrapper.vm; }); it(" ...

Using React hooks with Material-UI: Snackbar displaying only on first occasion and not again

I have identified an issue that can be easily reproduced. Steps to replicate: Step 1: Start a react app and include material-ui in the project: prompt> create-react-app mui-test prompt> cd mui-test prompt> yarn add @material-ui/core @material-ui ...

When attempting to transfer data from the parent component to child components, the data is appearing as undefined in the display

I have been working on passing data from a parent component to child components, but I keep encountering an issue where the data is showing as undefined. Below is the code snippet: Parent Component In this section, I have declared the variable part_data ...

"Utilizing VueJS to invoke the action attribute of an HTML form as a method: A step-by

I am currently working on building an app that enables users to upload images through a drag and drop interface, triggering actions specified in the form immediately. index.ejs <form class="form" id="form" method=" ...

Shifting attention to an angular 6 form field

I am developing an application in Angular which involves creating, reading, updating, and deleting user information. I have a requirement for the username to be unique and need to display an error message if it is not unique, while also focusing on the use ...