The error "res.json is not a function" has been encountered within the Next

I encountered some ambiguity in the Next.js documentation, which didn't provide much help for my issue - I'm facing challenges with the getStaticPaths function. Below is the code snippet causing trouble:

export async function getStaticPaths() {
    
    let res =  fetch("localhost:3000/api/main")
    const paths = res.json().map(state => ({
      params: {id: states.id},
    }))

    return {paths,
      fallback: false}
  }

Any insights on why this isn't functioning as expected? I also have a getStaticProps function that successfully loads res.json()

Answer №1

To accomplish this task, follow these steps:

  async function fetchStaticPaths() {
    const response = await fetch("http://localhost:3000/api/main");
    const data = await response.json();
    const paths = data.map(item => ({
      params: {id: item.id},
    }));
    return {
      paths,
      fallback: 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

Ways to have a component in Angular 5 patiently await and receive data from another component without the use of an event signal

Seeking assistance with a dilemma involving two components (pop ups) where I need to transfer data from a child component to a parent one that lacks an event to retrieve the data. Essentially, I am looking for a way for the parent to be able to listen fo ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

What is the best way to access a promise's value in global scope within a reactjs application?

Currently tackling user authentication using web tokens in react. My approach involves utilizing the fetch() method to send a POST request to my backend, facilitating CORS. The issue arises when attempting to employ the setToken() hook within the .then() b ...

Encountering an Unexpected Token Import Issue with Page Objects in Webdriver.io and Node.js

Hi, I've encountered an issue while attempting to run my test scripts using Node.JS and Webdriver.io. Everything was functioning correctly until I decided to implement the Page Object Pattern. I am now receiving an error in the console output: ERROR: ...

No traces of SOI could be detected in the stored canvas image

I have a unique project using Three.js, where I am enabling users to save diagrams they draw on a canvas as JPEG images. The process involves: <a id="download" download="PathPlanner.jpg">Download as image</a> function download() { var dt = ca ...

Tips on invoking the ('keyup') function within an AJAX request's success callback

I have been working on implementing a live search feature that is based on JSON data fetched through an AJAX call. Here is the code I have been using, but unfortunately, it is not functioning as expected. function returnVisitors(){ let output ...

Iframe seamless proxying for enhanced efficiency without any unnecessary additional traffic

In order to facilitate cross-domain javascript communications, I employ the iframe proxying technique along with monitoring the onresize event. You can find a detailed explanation of this method at . However, I am dissatisfied with the additional page lo ...

The v-for directive is displaying my list in a single row with multiple columns instead of in a single column with multiple rows

Can someone please assist in rendering my list as shown below: A B C The current rendering looks like this: 1: A 2: B 3: C Below is the code snippet: To-Do List: <input type="text" class = "todo" placeholder = "Next Item" v-on:keyup.enter="add ...

Tips for utilizing the 'contains' feature within web elements with Java Selenium WebDriver?

In my current project, I am facing a challenge with two specific elements: one is a string formatted as ABC £12,56 and the other is a box called "Cashbox" that should be 15% of the value of the string. However, when attempting to locate the CSS for both e ...

Find and delete an item from a JSON array

Currently, I am attempting to locate a specific object within a JSON array and remove it. The structure of my JSON array containing objects is as follows: var data = [{ {id: "1", name: "Snatch", type: "crime"}, {id: "2", name: "Witches of Eastwic ...

Guide on creating rsa key pair on the client side with angular2

Is there a way to generate an 'rsa' key-pair on the client-side using angular2? I am looking to create a private/public key pair and store the private key in a database while using the public key on the client side. How can this be achieved? I c ...

Ensure that the entire DIV is interactive, with the exception of the dropdown

I have successfully made the entire DIV clickable, but I am facing an issue with the dropdown menu. Whenever I click on the dropdown button, it not only opens up the dropdown menu but also quickly redirects to 'next.html'. Is there a way to preve ...

How to Implement Multiple OR Statements in SharePoint 2010 Using Jquery and JSON Rest

Struggling to create a multiple OR variable, but it just won't work. Is there an issue with my syntax or logic? var category = (i.Category == "Electric" ? "E" : i.Category == "Power Module" ? "PM" : i.Category == "Branch Office" ? "BO" : ""); After ...

Instruction failed to produce HTML output

Currently facing a challenge with my code - I have a dynamically created span (via ng-repeat). <span my-size id="abc"></span> <span my-size id="def"></span> <span my-size id="ghi"></span> The goal is to extract the id ...

The axios requests are sent to the backend API, but the webpage remains empty without

I am trying to retrieve a base64 encoded image from my local backend by making a local API call. The logging on the backend confirms that axios is successfully calling the API, however, the frontend displays an empty page with no data. What could be caus ...

Troubleshooting Problems with Fancybox Scaling on Small Landscape-Oriented Mobile Devices

Issue Description: In my responsive web design, I am utilizing Fancybox v2.1.5. The problem arises when smaller mobile devices with a more rectangular shape than square are used. In portrait orientation, when invoking Fancybox, it expands to the width of ...

Tips for extracting tables from a document using Node.js

When converting an XML document to JSON using the xml-js library, one of the attributes includes HTML markup. After parsing, I end up with JSON that contains HTML within the "description":"_text": field. { "name": { ...

Make sure to confirm the input length and utilize bootstrap validation for error checking

I have a rather straightforward question. I am currently utilizing Bootstrap 4.6 and am looking to implement form validation for my input field. The requirement is to validate whether the input has a length of 5 characters or less, in which case it should ...

What is the best way to mention @here with an attachment?

I'm attempting to use the canvas say command to display an image with an @here mention included, but unfortunately it only shows the image without making the mention. Below is what I have attempted: message.channel.send(`@here\n`+attachment); ...

cross-domain policy file

Currently, I am attempting to make a web service call from JavaScript using AJAX: $.ajax({ type: "GET", url: "http://[REMOTE-SERVER-IP]:8080/api/service", contentType: "application/jsonp", crossDomain: true, success: successFunc, ...