gpt4 book ai didi

next.js - 类型错误 : Cannot destructure property `req` of 'undefined' or 'null'

转载 作者:行者123 更新时间:2023-12-04 15:40:19 26 4
gpt4 key购买 nike

我正在尝试让名为“With Apollo Auth”的 Next.js 示例正常工作:https://github.com/zeit/next.js/tree/master/examples/with-apollo-auth

我设置了所有内容,但出现错误:

TypeError: Cannot destructure property `req` of 'undefined' or 'null'.

const { AppTree, ctx: { req, res }} = ctx <----- Error line

此错误来自以下页面,该页面是 Next.js 提供的样板代码:

import initApollo from './initApollo'

function parseCookies (req, options = {}) {
return cookie.parse(req ? req.headers.cookie || '' : document.cookie, options)
}

export default App => {
return class WithData extends React.Component {
// It is needed for better devtools experience. Check how react devtools shows it: "MyApp WithData"
static displayName = `WithData(${App.displayName})`

// Since apolloState is required but it is missed before this method returns the new props,
// so it is needed to provide defaults
static defaultProps = {
apolloState: {}
}

static propTypes = {
apolloState: PropTypes.object.isRequired
}

static async getInitialProps (ctx) {
const { AppTree, ctx: { req, res }} = ctx
const apollo = initApollo(
{},
{
getToken: () => parseCookies(req).token
}
)

ctx.ctx.apolloClient = apollo

let appProps = {}
if (App.getInitialProps) {
appProps = await App.getInitialProps(ctx)
}

if (res && res.finished) {
// When redirecting, the response is finished.
// No point in continuing to render
return {}
}

if (typeof window === 'undefined') {
// Run all graphql queries in the component tree
// and extract the resulting data
try {
// Run all GraphQL queries
await getDataFromTree(<AppTree {...appProps} apolloClient={apollo} />)
} catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop:
// https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error
console.error('Error while running `getDataFromTree`', error)
}

// getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually
Head.rewind()
}

// Extract query data from the Apollo's store
const apolloState = apollo.cache.extract()

return {
...appProps,
apolloState
}
}

constructor (props) {
super(props)
// `getDataFromTree` renders the component first, the client is passed off as a property.
// After that rendering is done using Next's normal rendering pipeline
// @ts-ignore
this.apolloClient = initApollo(props.apolloState, {
getToken: () => {
// @ts-ignore
return parseCookies().token
}
})
}

render () {
// @ts-ignore
return <App apolloClient={this.apolloClient} {...this.props} />
}
}
}

我不确定为什么 ctx 未定义。

最佳答案

你做错了解构,修复以下

const { AppTree, req, res } = ctx;

关于next.js - 类型错误 : Cannot destructure property `req` of 'undefined' or 'null' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58023665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com