Encountering difficulty in identifying the error during data fetching on the server side of a Next.js application

I am attempting to troubleshoot an error with my Next.js server-side data fetching, but I am encountering two problems.

export async function getStaticProps() {

  try {

    const fetchUrl = "some api url";

    const resp = await axios.get(fetchUrl);

    return { props: { articles: resp.data.articles } };

  } catch (err) {
    console.log(err)
    return { props: { articles: [], error: err } };

  }

}

The error object is not serializable and I am unable to view the console log result. I have tried converting the error to a string and received an output of object undefined.

When it reaches the catch block, I attempted to assign a "custom error message" to it, but I do not have access to the exception message within the catch block.

The official documentation for Next.js only mentions using custom messages for errors.

Answer №1

Encountering the Object is not serializable error indicates that the object cannot be converted into a JSON serialized format, potentially due to the complexity of the err object.

Based on your code, it appears to be an error originating from axios. To troubleshoot this issue, try sending err.message to retrieve the error message. For further guidance, refer to the axios documentation. It is important to note that undefined is not a valid value for serialization; in such cases, consider using null instead.

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

Creating a backup link for a video player component in NextJs

My aim is to make sure that two video player components in a NextJS application can still access and play videos even when the application is running locally using npm run dev without an internet connection. Currently, these two components.. <HoverVi ...

Trouble with Add To Cart feature in React app due to issues with Context API

I have been following a tutorial located here. I followed the steps exactly and even checked the code on the related github repository, which matches. However, when I try to add a product to the cart by clicking the button, the state does not update. In Re ...

An effective solution to address the Eslint warning regarding the prohibition of spreading specific props

Just getting started with React and I have a question. How can I address the Prop spreading is forbidden Eslint warning that keeps popping up? After reading through the Eslint documentation, it seems like I have to destructure all props like this: con ...

What is the most efficient method of storing Formik values in Redux Toolkit to ensure persistence using redux-persist?

I have created a multi-step form using React, Formik, Redux Toolkit, and redux-persist. The form steps are currently saved in the Redux state, allowing users to resume from where they left off even if they close or reopen the window. However, I am now faci ...

Button-Enhanced File Upload using Material-UI

I'm having some issues with creating a file upload feature triggered by a button click. <label> <input style={{ display: 'none' }} type="file" /> <Button variant="contained" color="defau ...

Embrace the expanding capabilities of React Material UI as it blossoms on

Is there a way to adjust the context menu so that it opens to the left of the button rather than in the center? I would like to achieve this using Material UI properties. An example can be found at: https://codesandbox.io/s/2f33z Current positioning: ht ...

What could be causing Next.js middleware to run repeatedly?

Recently, I set up a brand new Next.js project using npx create-next-app@latest --typescript. Upon completing the installation (version 13.3.4), without making any modifications to existing files, I introduced a new file named middleware.ts within the src ...

Executing a function right away when it run is a trait of a React functional component

Having a fully functional React component with useState hooks, and an array containing five text input fields whose values are stored in the state within an array can be quite challenging. The main issue I am facing is that I need to update the inputfields ...

Failure of React Library in Production Mode on Webpack 4

Encountering an issue while trying to compile my react library in mode: production. After importing the library into another application, the following error message is displayed: Uncaught TypeError: Cannot set property props of #<TWithProps> which ...

Error: Attempted to access the 'state' property of an undefined object

I am working with the following function: extractCountries: function() { var newLocales = []; _.forEach(this.props.countries, function(locale) { var monthTemp = Utils.thisMonthTemp(parseFloat(locale["name"]["temperature"])); if(p ...

Deciphering TS2345: "The argument supplied, known as 'typeof MyComponent', cannot be assigned to the specified parameter type"

I am facing an issue while attempting to integrate a Typescript React component with react-onclickoutside. The error message that I encounter is as follows: TS2345: Argument of type 'typeof MyComponent' is not assignable to parameter of type &apo ...

Reviewing and Implementing React and Material-UI Autocomplete for Selecting Multiple

Having trouble using Formik and MUI Autocomplete with multiple items. Specifically, when searching for "Pencil", it doesn't highlight the item. Also, you can select it twice by clicking on it. Desired outcome: Being able to select one or more items. ...

Transferring information in React from a child component to its parent and then to another child

I'm currently developing my app using react.js and facing an issue with passing data between components. I have a Parent component (P) that needs to receive an array of objects from its child component (C1), then pass this data on to another child com ...

Error encountered: CORS restriction preventing access when attempting to connect from local network

I am currently developing a web application where the backend retrieves data from a MongoDB and then the frontend displays it. Here is a simplified outline of the setup: Backend (ExpressJs) const app = express(); const port = process.env.PORT || 3001; a ...

Leveraging JavaScript packages without the need for npm or yarn

When working with React, I am able to incorporate the library simply by adding the following two lines of code: <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.c ...

Unable to locate module: '@material-ui/pickers' - Material UI React

I encountered an error that said: Material UI React - Module not found: Can't resolve '@material-ui/pickers' in React. Previously, I faced a similar issue with '@date-io/date-fns' but was able to fix it by updating to the latest ...

Enhancing the design of an HTML tag element with a custom style using ReactJS material-ui

I am currently working on a ReactJS file that incorporates an Icon from the Material-UI library. import LocalHospital from "@material-ui/icons/LocalHospital"; Later in the code, I am utilizing this icon within another HTML tag like so: <div c ...

Using SCSS to apply a class in Next.js

Currently, I am working on a project using Next.js and implementing the SCSS module procedure for styling. An example component directory structure is as follows: SampleComponent -> index.tsx and SampleComponent.module.scss The code in SampleComponent ...

Implementing the row delete function in MUI DataGrid using JavaScript

My grid has a delete icon button in each row, but it's not deleting the row. Can someone please help me with this issue? I'm working on a todo app where each row should have its own delete button. I'm struggling to connect the deleteTodo co ...

What steps can I take to personalize Material UI within a React application?

As someone who is new to this UI framework and React, I have been tasked with developing an application that requires more design patterns. I specifically chose a framework that does not depend on jQuery code. However, I am facing challenges when it comes ...