I encountered an issue with my promise where the data was undefined, causing me to have trouble properly setting the error. Could this be

Recently, I encountered an issue while trying to sign up my users using a simple form. An error popped up indicating that the data (promise argument) is undefined:

Unhandled Runtime Error TypeError: Cannot read property 'message' of undefined

(apologies for any technical jargon)

Frontend

import React,{useState} from "react";
import {signup} from '../../actions/auth'

const SignupComponent = () =>{
   const [values, setValues] = useState({
      name: "Fares",
      email: "example@example.com",
      password: "password123",
      error: "",
      loading: false,
      message: "",
      showForm: true,
   });

    //rest of the code remains the same...

export default SignupComponent;

Signup method (isomorphic-fetch)

import fetch from 'isomorphic-fetch';
import {API} from '../config'

//rest of the code remains the same...

exports.signup = (user)=>{
    return fetch(`${API}/signup`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Contnet-Type": "application/json",
      },
      body: JSON.stringify(user)
    }).
    then((response) => {
      return response.json();
    }).catch((error) => console.log(error));
}

Auth API

// code omitted for brevity

// FrontEnd dependencies

"dependencies": {
    // dependencies listed here...
  },

backend dependencies

"dependencies": {
    // dependencies listed here...
  }

Answer №1

Your registration process is asynchronous, so it should utilize a promise for handling. If you are already managing the .then method in your handleSubmit function, there is no need to include it in the signup process. Here is the revised code snippet:

import fetch from 'isomorphic-fetch';
import {API} from '../config'

// considering that fetch returns a promise and the signup function also returns a promise
// we can now use .then and .catch on the returned promise accordingly
export const signup = async (user) => {
    return fetch(`${API}/signup`, {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      body: JSON.stringify(user)
    })
}

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

Enforcing HTTPS in Next.js version 13

I've encountered a challenge while working with Next.js 13 and Heroku - there seems to be limited information on handling SSL with this combination. Although I have implemented a solution derived from this blog post below, Google is not happy with the ...

Can anyone explain why I keep encountering an endless loop when using react hooks?

Having a bit of trouble with fetching data from a JS file that is mimicking an API with some endpoint methods. When I console log the data, everything seems fine, but for some reason, I keep running into an infinite loop when using hooks. I've tried t ...

Having issues implementing CssBaseline

I referred to the solutions provided in this thread but encountered an error. TypeError: Object(...) is not a function const theme = createMuiTheme(); My goal is to apply default styles using <CssBaseline />. How can I achieve this? Below is my imp ...

Can you explain the significance of response.on in Node.js?

Currently facing an issue with a Node.js http request. If I can't figure it out, there's a bigger question that I'll address later. I've been working on some code and came across 'response.on', but I'm not entirely sure ...

Struggling with making react-hook-form correctly validate an email address

I've been struggling for a long time to make this validation work correctly, but it seems impossible. I even added some text at the bottom to display an error message related to the email, but it always shows no error, regardless of the situation. Ed ...

Ways to utilize a single node_modules folder across multiple projects

Is there a simple method to centralize and share one node_modules folder among all Angular projects, thereby preventing the need to download the same dependencies each time we start a new project? If so, is this approach recommended (what are the pros and ...

When using the DateTimePicker component in MUI with React, the displayed value may not always match the value that

When passing a value to the DateTimePicker component from '@mui/x-date-pickers/DateTimePicker, the displayed value sometimes shows a discrepancy of +2 hours or +1 hour. This issue may be related to timezones. For example: The value being passed: &apo ...

Utilizing TypeScript for dynamic invocation of chalk

In my TypeScript code, I am trying to dynamically call the chalk method. Here is an example of what I have: import chalk from 'chalk'; const color: string = "red"; const message: string = "My Title"; const light: boolean = fa ...

Encountered an error while attempting to run npm install for angular2-tree-component

I recently attempted to install the angular2-tree-component, but encountered a failure in the process. As I am still gaining experience with this technology stack, I would greatly appreciate some assistance in resolving this issue... /R/VKS/oryol/oryo ...

Tips for accessing the app instance within a module in Nest.js

Currently, I am involved in a project that relies on multiple Nest repositories, approximately 4 in total. Each repository must integrate logging functionalities to monitor various events such as: Server lifecycle events Uncaught errors HTTP requests/resp ...

I encountered a 404 Error message while attempting to access a specific express route

My Angular CLI generated app is running with an Express.js and MongoDB server. After running npm start, I can access http://localhost:3000, which routes to my homepage. The other links on the navbar work well to control routes, e.g., http://localhost:3000/ ...

Transferring information among React Native screens (From Screen A to Screen B to Screen C)

Imagine this particular situation. Essentially, a MaterialTopTabNavigator is nested within a StackNavigator. The challenge at hand is how to effectively pass data from a function all the way to the MaterialTopTabNavigator. Keep in mind that the data has t ...

Attention: validation of DOM nesting encountered an error: element <div> is not permitted to be nested inside <p>

I am facing the issue of not being able to find a solution to a known problem. The paragraph in question must not contain any other tags, yet I did not use any paragraph tags in my case. return ( <div className={classes.root}> <Stepper acti ...

Automatically redirect users to a new page using ReactJs/NextJS after a brief delay of 5 seconds

After a customer logs in, I want to show them a welcome page that automatically redirects them to another page after 5 seconds. To achieve this, I initially set the state with a default value of 5 seconds and then used setInterval to count down by 1 every ...

Establishing a connection between two Node JS servers

I encountered an issue with my sample project where I implemented Google recaptcha. The challenge I faced was verifying the captcha response at the back end using Node JS. While using the request module in NodeJS to connect with the Google server, I receiv ...

At what point is it ideal to initiate a project?

Imagine my project as an npm package where I use tools like webpack or grunt to bundle my css, js, and img files into a dist directory. The build script is specified in the package.json. When is the best time to execute the build script? Should I do it du ...

What is the process of generating enum values using ES6 in Node.js?

Can JavaScript support enumerations with assigned integer values, similar to how other programming languages handle it? In C#, for instance, I can define an enum as follows: enum WeekDays { Monday = 0, Tuesday =1, Wednesday = 2, Thursday ...

"Revolutionizing online payments with Material UI's dynamic payment

Struggling with creating dynamic material UI payment elements, I wanted to share how I successfully achieved this. You can access the code for these elements on the material UI website. https://i.stack.imgur.com/kYewa.png Below is an example of static da ...

Why isn't the hover function working on the table row element?

When I try to apply a hover effect on tbody and td elements, it works perfectly. However, when I apply the same effect to the tr element, it doesn't work as expected. I am using inline style (js pattern) rather than CSS code and incorporating Radium i ...

Is it possible to incorporate AngularJS 1.4, AngularJS 2.0, and ReactJS all within the same project?

My project is a collection of tags that are referred to by different names in various languages. I refer to these elements as views. Currently, our users are creating views using Angular 1.4. I am looking to provide flexibility to our users so they can ...