When implementing dynamic routing in Next.js, an error occurs with TypeError: the 'id' parameter must be a string type. It is currently

I’m encountering a problem while creating dynamic pages in Next.js. I'm fetching data from Sanity and I believe my code is correct, but every time I attempt to load the page, I receive a type error - “the ‘id’ argument must be of type string. Received null”. The file path specified is ‘/post/[slug].js’, so I have consistently used the variable name 'slug' throughout my code. I'm unsure where this 'id' is coming from or if there's an additional parameter I need to pass, or if I am not passing the slug correctly!

I sought help in the Sanity forums, and it doesn't seem to be an issue with the API - I can successfully retrieve data in other parts of the application without any problems. In the console, it appears that the page compiles without errors, but this specific error occurs when trying to load it. I even tried having an empty div in the page component to eliminate any issues related to the presentation logic, but unfortunately, that did not resolve the problem.

The complete error message has been shared in a gist, and it seems to indicate a potential problem with jest or next-server. Despite several attempts, I am unable to identify the root cause of this issue!

import client from '../../client’

const Post = ({ post }) => {
  ...
}

export default Post

export const getStaticPaths = async () => {
  const posts = await client.fetch(`*[_type == "blog"]{ slug }`)
  const paths = posts.map(post => ({
    params: {
      slug: post.slug.current
    }
  }))
  
  return { 
    paths,
    fallback: false }
}

export const getStaticProps = async ({ params }) => {
  const post = await client.fetch(`*[_type == "blog" && slug.current == $slug]{ title, date, link, excerpt, body, slug }[0]`, {slug: params.slug})
  
  return {
    props: { post }
  }
} 

UPDATE: It appears that this issue may be related to play.js or its configuration settings - conducting the same operation on a desktop environment does not produce the same error.

Answer №1

The problem might be related to the file name. Make sure the name is [slug].js and it is placed in the directory pages/post. If you access the page through the URL

http://localhost:xxx/post/slug-to-be-inserted-here
, everything should work smoothly.

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

The menu options pulled from a JSON file generated on the server using Nextjs are not showing up in the HTML source code

Apologies if the title is a bit unclear. Let me try to explain: my website is built using Next.js and retrieves data from Sanity Studio (CMS). In Sanity, users can create menu items which are displayed in the footer component, sidebar component, and header ...

Incorporate the JavaScript file fetched from NPM into the app.js code

New to using node and npm, I recently set up my main JavaScript file called app.js and configured webpack nicely. Inside app.js, I currently have the following script: //require in jquery var $ = require('jquery'); //require a constructor f ...

What is the best method to smoothly navigate to a specific id on a webpage after conducting a search using

I want to implement a jQuery function that allows me to scroll down to an id containing search results. The script as a whole, minus the JS, is functioning properly. I just need assistance in creating a method to search for specific id values. Below is my ...

Issue with Inline JS not functioning correctly in Flask when integrated with Bootstrap 5

I'm developing a Flask web app with Bootstrap 5 and attempting to incorporate inline JS, but it's not functioning as expected. Specifically, I'm trying to use a simple alert() component, but nothing is displaying on the page. Interestingly ...

Updating the src of an iframe and adding an onclick event to a link using JavaScript

Looking to design a page that accommodates both login and sign up functionalities by dynamically updating the src of an iframe. In the navigation bar, there is: <li id="change"><a onclick="sign()">Sign up</a></li> And within the ...

Transform and command the data acquired through an AJAX request using jQuery

My code includes an AJAX call using jQuery: $('.add a').click(function() { $.ajax({ type: 'POST', url: '/api/fos', context: this, datatype: 'html', success: function(data) ...

Fetching chat messages using an AJAX script depending on the timestamp they were sent

I am currently working on developing a real-time chat application with various rooms as part of my project. Progress has been smooth so far, but I am facing an issue with properly fetching the most recent chat messages from the database. The message table ...

Local variables in AngularJS across multiple Angular applications

Looking for a method to retain a local variable not affected by path or angular app changes. Attempted using $window.localStorage.set and get item, rootScope, and $window.variable with no success. ...

Combining the `vuex-typex` npm package's `dist/index.js` for optimal performance with Jest testing framework

I initially raised this question as an open issue on GitHub. My experience with Vue.js, Vuex, TypeScript, and vuex-typex has led me to encounter syntax errors during Jest testing. I am relatively new to working with Vue.js, TypeScript, and Jest. It is wo ...

ToggleGroup component causing type mismatch alert

I'm encountering an issue with the ToggleGroup component from the shadcnui library in my Next.js 14 project. Specifically, when I try to set the type property as either "single" or "multiple", I face a type incompatibility problem. The ToggleGroup com ...

Having Trouble Showing Loading Component on Next.js v13

Having some issues with setting up a loading component in my Next.js v13 project. I followed the documentation by creating a file called loading.tsx in the src directory, but it's not appearing on the page as expected. I've also included a functi ...

Rejuvenating a template generated on the server in AngularJS

I have a specific partial which is a report - simply a static list of names and dates designed for viewing and printing purposes. For efficiency reasons, the report is rendered server-side, so when a report request is made, my API responds with HTML rathe ...

What is causing the server not to function properly in Next.js 13.4?

I created a function where I declared the use of server with the following code, According to the documentation, cookies can only be modified in a Server Action or Route Handler. For more information, visit: https://nextjs.org/docs/app/api-reference/funct ...

Struggling with adding documents into mongoDB with the help of mongoose and node.js

I have a mongoose model defined below: module.exports = mongoose.model('vbDetail', { company_name: String, rowsdata: {vals: { date: Date, transaction_type: String, transaction_num: Str ...

JavaScript - Uncaught ReferenceError: WebSocket is undefined

Looking at just the client side API (since each server side language has its own API), this code snippet demonstrates opening a connection, setting up event listeners for connect, disconnect, and message events, sending a message to the server, and closing ...

``There seems to be an issue with the functionality of JSON.stringify

Upon attempting to use the JSON.stringify() method to convert an array of strings into a JSON object for passing to a PHP script, I encountered an issue where the method did not return any meaningful output. The code provided is the only one handling the ...

What is the best way to display Mongoose schema validation error messages on the client side in a Next.js application?

Trying to implement user registration form in a Next.js app with MongoDB. I have set up a user schema that includes a username field with the unique: true flag and a custom validation error message. In my RegisterForm component, I am attempting to display ...

Is there a way to adjust the height pixel value in my code so it can be dynamic?

I have created a simple script that allows selected objects to fade in as the user scrolls down. However, my issue is that this script is quite rigid. If I were to apply it to 20 different objects, for example, I would need to manually adjust the height ea ...

Issue with ng-show directive in AngularJS

Currently, I am developing a web app using AngularJS 1.6.6. In my template, I have implemented ng-show in order to recycle it: <div > <br/> <div class="form"> <form data-ng-submit="objectHandlerVM.functions.objectHandl ...

Utilizing React and Material-UI to Enhance Badge Functionality

I am exploring ways to display a badge icon when a component has attached notes. I have experimented with three different methods and interestingly, the results are consistent across all of them. Which approach do you think is the most efficient for achiev ...