简体   繁体   English

NextJS getInitialProps 没有达到我的预期,我收到编译器错误“varName not found on type {}”

[英]NextJS getInitialProps is not passing how I would expect and I get compiler error "varName not found on type {}"

I think I am having a simple Syntax issue that is roadbloacking me hard on NextJs right now.我想我有一个简单的语法问题,现在在 NextJs 上阻碍了我。

I'm trying to do some dynamic server side fetches so I tried to the getInitialProps pattern but the compiler can't recognize the return of getInitialProps in the normal functional component render:我正在尝试做一些动态服务器端提取,所以我尝试了 getInitialProps 模式,但编译器无法识别正常功能组件渲染中 getInitialProps 的返回:

interface Props {
    date: string,
    user: number
}


const DayTimeView: NextComponentType<Props> = ({timeRecords, date, user}) => {

    return (
        <div>
            <h1>DayTimeView</h1>
        </div>
    )
}

DayTimeView.getInitialProps  = async (props: Props) => {
    let sesh = getSession();
    let timeRecords = []
    getSession().then((session) => {fetch('http://localhost:3000/api/time/date/' +new Date(props.date).toISOString(), {
        method: 'GET',
        headers: { "Content-Type": "application/json",
                    "x-auth-token": session.user.id},
        })
    .then(res => res.json().then(data => {
        if (data.length > 0) {
            timeRecords = data;
        }else{
            timeRecords = []; // Todo default
        }
    }))})
    return {props: { timeRecords: timeRecords, date: props.date, user: props.user }}
    
}

export default DayTimeView;

Here is the compiler error:这是编译器错误: 错误

And it's not the fact timeRecords doesn't exist in props because it doesn't work even when removing timeRecords for the same error on date and user.这并不是 timeRecords 不存在于道具中的事实,因为即使在删除 timeRecords 时对于日期和用户的相同错误它也不起作用。

DayTimeView should be of type NextPage , which is a NextComponentType that also includes a NextJS context. DayTimeView应该是NextPage类型,它是一个NextComponentType ,它还包含一个 NextJS 上下文。

import { NextPage } from 'next';
const DayTimeView: NextPage<Props> = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NextJS和next-routes不像我期望的那样进行路由 - NextJS and next-routes not routing as I would expect 为什么我在第一次加载时在 nextjs 中没有得到任何东西(getInitialProps 中的 css 和 params) - why i dont get nothing in nextjs in first load (css and params in getInitialProps) 如何将自定义参数传递给 nextjs 动态路由,以便我可以在 getInitialProps 中访问它 - How do I pass a custom parameter to nextjs dynamic route so I can access it inside getInitialProps nextjs getInitialProps() 没有将道具传递给 class 组件 - nextjs getInitialProps() not passing props to class components Nextjs getInitialProps 错误阻止在 Netlify 上构建 - Nextjs getInitialProps error preventing build on Netlify nextjs redux,thunk和getInitialProps - 如何实现 - Nextjs redux, thunk and getInitialProps - how to implement 连接 NextJS、next-i18next、with-redux、with-redux-saga:“错误:如果您的自定义 _app.js 文件中有 getInitialProps 方法...” - Connecting NextJS, next-i18next, with-redux, with-redux-saga: "Error: If you have a getInitialProps method in your custom _app.js file..." 将道具传递给孩子时 NextJS 类型错误 - NextJS type error while passing props to children 反应中的 onChange 没有按我预期的那样工作 - onChange in react not working as I would expect 如何在 nextjs 中获取子组件? - How can i get children component in nextjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM