Jowz
Jowz

Reputation: 496

RTK Query Error handling/ _a2 is undefined?

I' have many different RTK Query calls made throughout my app, and they all have been working fine (get, post, patch, delete). Yet on my current component with a post call, I'm getting an error that I have not seen before and cannot figure out what is tripping it up. The error I'm getting is name: "TypeError", message: "_a2 is undefined", followed by a long stack starting with fetchBaseQuery. The call to the backend works fine on Postman, I have no props or fields defined as _a2 (or anything close to that), the stack trace has no reference to an _a2, and I cannot find anything online referencing the same error. Is this something that anyone has seen?

I tried getting more detail from the error based on the suggestions from the docs, but these all return undefined. So maybe not an RTK Query error? If not though, I'm at a total loss. https://redux-toolkit.js.org/rtk-query/usage/error-handling

I have my set up below, in case I'm just completely overlooking a simple syntax issue.

apiSlice call:

addGoal: builder.mutation({
            query: (arg) => {
                const {planId, values} = arg;
                return {
                url: `/planapi/goals/${planId}`,
                method: 'POST',
                body: values
                }, console.log(values)
            },
            invalidatesTags: ['Goals']
        }),

the component making the call:

const AddGoal = ({planId}) => {
  
  const [addNewGoal, { error }] = useAddGoalMutation()
    
  let rtkerror
  if (error) {
        console.log("error", error.status, error.data) //returns undefined
        rtkerror = <div><h3>{error.status} {JSON.stringify(error.data)}</h3></div>
       //nothing is rendered from this, probably because coming back undefined?
    }


  const [open, setOpen] = React.useState(false);
  console.log("planId ", planId)


  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };
   
  return (
  <div>
    <Button variant="contained" onClick={handleClickOpen}>
      Add Goal
    </Button>
    <Dialog open={open} onClose={handleClose}>
      <DialogTitle>Add New Goal {rtkerror} </DialogTitle>
      <DialogContent>
       
  
  <Formik
    initialValues={{
      title: '',
      dataCollection: '',
      staffRole: '',
      frequency: '',
      location: '',
      projectedHours: '',
      materials: '',
      currentStatus: '',
      quote: '',
      measurement: '',
      plan: ''
    }}
    validationSchema={validationSchema}
  
    onSubmit={async (values) => {

      console.log(values);
      
      try {
      const payload =  await addNewGoal({ planId, values })
      .unwrap()
      .then((payload) => console.log('fulfilled', payload)) //this is also logging fulfilled undefined
      .catch((error) => console.error('rejected', error)); //I've tried as error.status and error.data but these return undefined.
        console.log('fulfilled', payload)
      } catch (err) {
        console.error('Failed to save goal: ', err)
        toast.error("Error Adding Goal", {
          position: toast.POSITION.TOP_CENTER
        });
      }

      toast.success("Goal Added", {
        position: toast.POSITION.TOP_RIGHT
      });
      //handleClose();
      
    }}>
  
 
    {formik => (
      <form onSubmit={formik.handleSubmit}>
        <TextField
          id="title"
          name="title"
          label="Title/ Name of Goal"
          placeholder=''
          value={formik.values.title}
          onChange={formik.handleChange}
          error={formik.touched.title && Boolean(formik.errors.title)}
          helperText={formik.touched.title && formik.errors.title}
        />
        <TextField         
          id="dataCollection"
          name="dataCollection"
          label="Data Collection"
          placeholder=''
          value={formik.values.dataCollection}
          onChange={formik.handleChange}
          error={formik.touched.dataCollection && Boolean(formik.errors.dataCollection)}
          helperText={formik.touched.dataCollection && formik.errors.dataCollection}
        />
        <TextField
          id="staffRole"
          name="staffRole"
          label="Staff Role"
          placeholder=""
          value={formik.values.staffRole}
          onChange={formik.handleChange}
          error={formik.touched.staffRole && Boolean(formik.errors.staffRole)}
          helperText={formik.touched.staffRole && formik.errors.staffRole}
        />
        <TextField         
          id="frequency"
          name="frequency"
          label="Times/Month"
          placeholder=""
          value={formik.values.frequency}
          onChange={formik.handleChange}
          error={formik.touched.frequency && Boolean(formik.errors.frequency)}
          helperText={formik.touched.frequency && formik.errors.frequency}
        />
        <TextField         
          id="location"
          name="location"
          label="Location"
          placeholder=""
          value={formik.values.location}
          onChange={formik.handleChange}
          error={formik.touched.location && Boolean(formik.errors.location)}
          helperText={formik.touched.location && formik.errors.location}
        />
        <TextField         
          id="projectedHours"
          name="projectedHours"
          label="Projected Hours"
          placeholder=""
          value={formik.values.projectedHours}
          onChange={formik.handleChange}
          error={formik.touched.projectedHours && Boolean(formik.errors.projectedHours)}
          helperText={formik.touched.projectedHours && formik.errors.projectedHours}
        />
        <TextField         
          id="materials"
          name="materials"
          label="Materials Used"
          placeholder=""
          value={formik.values.materials}
          onChange={formik.handleChange}
          error={formik.touched.materials && Boolean(formik.errors.materials)}
          helperText={formik.touched.materials && formik.errors.materials}
        />
        <TextField         
          id="currentStatus"
          name="currentStatus"
          label="Current Status"
          placeholder=""
          value={formik.values.currentStatus}
          onChange={formik.handleChange}
          error={formik.touched.currentStatus && Boolean(formik.errors.currentStatus)}
          helperText={formik.touched.currentStatus && formik.errors.currentStatus}
        />
        <TextField         
          id="quote"
          name="quote"
          label="Client Statement"
          placeholder=""
          value={formik.values.quote}
          onChange={formik.handleChange}
          error={formik.touched.quote && Boolean(formik.errors.quote)}
          helperText={formik.touched.quote && formik.errors.quote}
        />
        <TextField         
          id="measurement"
          name="measurement"
          label="Statement of Measurement"
          placeholder=""
          value={formik.values.measurement}
          onChange={formik.handleChange}
          error={formik.touched.measurement && Boolean(formik.errors.measurement)}
          helperText={formik.touched.measurement && formik.errors.measurement}
        />
        <TextField         
          id="plan"
          name="plan"
          label="Plan"
          placeholder=""
          value={formik.values.plan}
          onChange={formik.handleChange}
          error={formik.touched.plan && Boolean(formik.errors.plan)}
          helperText={formik.touched.plan && formik.errors.plan}
        />
        <DialogActions>
          <Button onClick={handleClose}>Cancel</Button>
          <Button color="primary" variant="contained" fullWidth type="submit">
          Submit
        </Button>
      </DialogActions>
      </form>
    )}
      </Formik>
      </DialogContent>
      
    </Dialog>
    </div>
  );
};

I attempted to get more details from the error in a couple of spots, but the only one that logs anything is the .catch after the call and .unwrap() (which is what provided the error referenced above). When I tried error.status or error.data, I just get undefined. But logging the error I get the stack trace with the _a2 is undefined.

Upvotes: 1

Views: 878

Answers (1)

phry
phry

Reputation: 44078

Due to you attaching , console.log(values) after the return statement, your query function returns undefined. That might already be your problem - can you remove that?

Upvotes: 1

Related Questions