fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

getInitialProps() becomes undefined #227

Closed KosukeYoshimura closed 1 year ago

KosukeYoshimura commented 6 years ago

Hello !

I try to get params by getInitialProps().

However, when I try to get that like this,

/pages/books/show.js

static async  getInitialProps(props) {

        console.log(props.query)

    }

it turned error like this.

"Show.getInitialProps()" should resolve to an object. But found "undefined" instead.

routes.js↓

const routes = require('next-routes')();

routes
    .add('/books/peterThiel', '/books/peterThiel')
    .add('/:name/books/show', '/books/show');  <----- want to use this "name" params

module.exports = routes;

Can anyone give me advice?

msreekm commented 6 years ago

i think you have return from getinitialprops static async getInitialProps(props) { console.log(props.query) return {name: props.query.name}

}
KosukeYoshimura commented 6 years ago

do u mean I did not return name params?

msreekm commented 6 years ago

i meant you have to return something from getInitialProps(0 method. https://github.com/zeit/next.js#fetching-data-and-component-lifecycle

lauriskuznecovs commented 5 years ago

You can try at least:

static async getInitialProps ({ query }) {
    return await query
}
hieunguyendut commented 4 years ago

I'm facing the same problem. I return a pathname from getinitialProps() functions but It showed undefined in render() function. Any one have answer for this issue?

hieunguyendut commented 4 years ago

static async getInitialProps ({ query }) { return await query }

I tried but still show undefined

lauriskuznecovs commented 4 years ago

static async getInitialProps ({ query }) { return await query }

I tried but still show undefined

Which Next.js version are you using?

Babatunde50 commented 4 years ago

I think you need to return an object. It can be an object of any kind, you just need to return an object for getInitialProps to work.