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

Is Typescript syntax for a collection of strings comparable to using string[]?

When working with Typescript, the convention to define an Array of Strings is either string[] or Array<string>. In our team, we lean towards using the more concise string[]. However, when it comes to defining a Set of Strings, is there a shorter syn ...

Exploring methods for testing React components with TypeScript's props

I am currently tackling a react-typescript project and I am looking to conduct testing on props passed to a react component using react-testing library. Here, we have the SharedDashboardUiLatestValueWidget.tsx component: export interface SharedDashboardU ...

Verify the specific type conditions of a key value in a mapped type

I am attempting to achieve the following: If the actions property exists, and there are callbacks within the actions properties that return a string, then return X or Y. The above code is expressed as: // type MappedType<T> = { // [K in keyof T]: ...

Guide on how to create a custom response using class-validator in NestJS

Is it feasible to customize the error response generated by class-validator in NestJs? The default error message structure in NestJS looks like this: { "statusCode": 400, "error": "Bad Request", "message": [ { "target": {} ...

Outputting undefined values when processing an http post array

I seem to have encountered a major issue. Despite my efforts, I am seeing an undefined value when trying to display this JSON data. {"StatusCode":0,"StatusMessage":"OK","StatusDescription":{ "datas": [ {"sensor_serial":"SensorSerial1", "id":"11E807676E3F3 ...

Empty Array returned in VueJS after Axios GET request | VUEJS/LARAVEL

Currently, I am working on a project for my university while also learning how to build a single page application through a Udemy course. The issue I'm facing is related to pushing data from a JSON database query named "alunos" to the front end using ...

Trouble with Nested Routing in React Router Version 6: Route not Rendering

I'm facing issues nesting a path within another path in react-router-dom version 6. When I try to visit the nested argument's page (/blog/1), it shows up as a blank non-styled HTML page. However, when I add a child path to the root like /blog, it ...

Removing excess space at the bottom of a gauge chart using Echarts

After trying to implement a gauge chart using Baidu's Echarts, I noticed that the grid properties applied to other charts are working fine, but the bottom space is not being removed in the gauge chart. Even after applying radius(100%), the space at th ...

What causes a component to not update when it is connected to an Array using v-model?

Array1 https://i.stack.imgur.com/cY0XR.jpg Array are both connected to the same Array variable using v-model. However, when changes are made to Array1, Array2 does not update. Why is this happening? Process: Upon examining the logs, it can be observed th ...

The content will only be visible when a comment is added and then removed

Currently, I am in the process of implementing an expansion panel that triggers an API call upon being clicked. This call then populates the v-card and v-layout components with the response data. However, a challenge I am encountering is that initially, th ...

Creating a Vue.js component that integrates a Bl.ocks.org graph

I'm a rookie in the world of D3 and I am trying to implement this cool d3 element into my Vue.js component. However, I've encountered an issue with the periodic rotation that I require not functioning properly. It seems to work initially but then ...

Adding images to your SVG using Bobril is a simple process that can add visual

I have been attempting to insert an image into an SVG using Bobril, but the following code is not functioning as expected: { tag: 'svg', children: { tag: 'image', attrs: { 'xlink:href': &ap ...

Explanation of Default Export in TypeScript

I recently started learning about JS, TS, and node.js. While exploring https://github.com/santiq/bulletproof-nodejs, I came across a section of code that is a bit confusing to me. I'm hoping someone can help explain a part of the code. In this project ...

Exploring Vue.js: Navigating through child components from parent component

I currently have 3 components in my project: Form, Card, and Button. Let's start with the Button.vue component: <template> <div> <slot v-bind="{ text }"> <button>{{ text }}</button> </slot> ...

The error message "Can't resolve all parameters for CustomerService" is preventing Angular from injecting HttpClient

I have a customerService where I am attempting to inject httpClient. The error occurs on the line where I commented //error happens on this line. Everything works fine until I try to inject httpClient into my service. The error message is: `compiler.js: ...

What is the best way to modify an existing object in an Observable Array in Angular?

As I work on my Ionic 5 / Angular application, a challenge arises when attempting to update a Conversation object within the existing array of Conversation: private _conversations = new BehaviorSubject<Conversation[]>([ new Conversation( & ...

Implementing conditional styling in Vue.js using v-bind based on specific Bootstrap breakpoints

Is there a way to dynamically assign styles based on different bootstrap breakpoints using v-bind style directive with vue? I'm encountering an error in the console that says "Whitespace was expected." <div class="footer" :style="{$b ...

What is the best way to convert a string to an integer in JavaScript while still maintaining compatibility with Internet Explorer 11?

Here is the code snippet that I am working with: setCol (param) { // missing forEach on NodeList for IE11 if (window.NodeList && !NodeList.prototype.forEach) { NodeList.prototype.forEach = Array.prototype.forEach; } const a ...

Container that has the ability to store objects conforming to specific interfaces

If you were to envision having three different types of objects, they might look something like this: interface X { testX: someType; } interface Y { testY: someOtherType[]; } interface Z { testZ1: string; testZ2: number; } Now imagine a master o ...

Creating custom password confirmation validation in Vue.js using VeeValidate

In my current project, I have successfully implemented vee-validate with custom components. However, I am encountering difficulties when trying to set up a Password Confirmation input field. Within my custom components, such as <base-input-field>, ...