首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Next.js getServerSideProps

Next.js getServerSideProps
EN

Stack Overflow用户
提问于 2022-11-29 16:49:38
回答 1查看 30关注 0票数 1

我的代码是,控制台发出的警告是附加的。如果你能告诉我问题出在哪里.

当我使用硬编码作为道具的“数据”时,我可以在组件中显示它们,但是当我尝试从api获取数据时,我可以得到数据,但不能将其传递给ui。

代码语言:javascript
复制
import numeral from 'numeral';
import { Box, Card, Grid, Typography } from '@mui/material';
import { useTheme } from '@mui/material/styles';
import { Chart } from '../../chart';
import { useEffect } from 'react';


const ChartLine = () => {
  const theme = useTheme();



  const chartOptions = {
    chart: {
      background: 'transparent',
      toolbar: {
        show: false
      },
      zoom: {
        enabled: false
      }
    },
    colors: ['#2F3EB1'],
    dataLabels: {
      enabled: false
    },
    fill: {
      opacity: 1
    },
    grid: {
      show: false
    },
    stroke: {
      curve: 'smooth',
      width: 3
    },
    theme: {
      mode: theme.palette.mode
    },
    tooltip: {
      enabled: false
    },
    xaxis: {
      labels: {
        show: false
      },
      axisBorder: {
        show: false
      },
      axisTicks: {
        show: false
      }
    },
    yaxis: {
      show: false
    }
  };

  const chartSeries = [{ data: [256, 282, 221, 245, 235, 274, 234, 256] }];

  return (
    <Chart
      options={chartOptions}
      series={chartSeries}
      type="area"
    />
  );
};


  

// const data = {
//   "Last1WeekQuantity": 0,
//   "Last1WeekAmount": 0,
//   "Last1MonthQuantity": 3,
//   "Last1MonthAmount": 73,
//   "Last3MonthQuantity": 3,
//   "Last3MonthAmount": 73
// };

export const getServerSideProps = async () => {
  const res = await fetch('http://localhost:8080/api/hbreport');
  const data = await res.json();
  {console.log("consolelog getServerSideProps")}
  console.log(data);

  return {
    props : {
      data
    } 
}
}

export const FinanceOverview  = (data) => {
  useEffect(() => {
    getServerSideProps();
  }, []);


  return (
    <Card {...data}>
      {console.log("consolelog under card")}
      {console.log(data)}
    <Grid container>
      <Grid
        item
        md={4}
        xs={12}
        sx={{
          alignItems: 'center',
          borderRight: (theme) => ({
            md: `1px solid ${theme.palette.divider}`
          }),
          borderBottom: (theme) => ({
            md: 'none',
            xs: `1px solid ${theme.palette.divider}`
          }),
          display: 'flex',
          justifyContent: 'space-between',
          p: 3
        }}
      >
        <div>
          <Typography
            color="textSecondary"
            variant="overline"
          >
            Last Week
          </Typography>
          <Typography variant="h5"> 
            {data.Last1WeekQuantity} orders
          </Typography>
          <Typography
            color="textSecondary"
            variant="body2"
          >
            
            {numeral(data.Last1WeekAmount).format('$0,0.00')} 
            &nbsp;
            last week
          </Typography>
        </div>
        <Box
          sx={{
            alignItems: 'center',
            display: 'flex',
            height: 54,
            width: 177
          }}
        >
          <ChartLine />
        </Box>
      </Grid>
      <Grid
        item
        md={4}
        xs={12}
        sx={{
          alignItems: 'center',
          borderRight: (theme) => ({
            md: `1px solid ${theme.palette.divider}`
          }),
          borderBottom: (theme) => ({
            xs: `1px solid ${theme.palette.divider}`,
            md: 'none'
          }),
          display: 'flex',
          justifyContent: 'space-between',
          p: 3
        }}
      >
        <div>
          <Typography
            color="textSecondary"
            variant="overline"
          >
            Last Month
          </Typography>
          <Typography variant="h5">
            {data.Last1MonthQuantity} orders
          </Typography>
          <Typography
            color="textSecondary"
            variant="body2"
          >
          
            {numeral(data.Last1MonthAmount).format('$0,0.00')}
            &nbsp;
            last month
          </Typography>
        </div>
        <Box
          sx={{
            alignItems: 'center',
            display: 'flex',
            height: 54,
            width: 177
          }}
        >
          <ChartLine />
        </Box>
      </Grid>
      <Grid
        item
        md={4}
        xs={12}
        sx={{
          alignItems: 'center',
          display: 'flex',
          justifyContent: 'space-between',
          p: 3
        }}
      >
                      <div>
                      <Typography
                        color="textSecondary"
                        variant="overline"
                      >
                        Last 3 Month
                      </Typography>
                      <Typography variant="h5">
                        {data.Last3MonthQuantity} orders
            
                      </Typography>
                      <Typography
                        color="textSecondary"
                        variant="body2"
                      >
                        
                        {numeral(data.Last3MonthAmount).format('$0,0.00')}
                        &nbsp;
                        last 3 month
                      </Typography>
                    </div>

        <Box
          sx={{
            alignItems: 'center',
            display: 'flex',
            height: 54,
            width: 177
          }}
        >
          <ChartLine />
        </Box>
      </Grid>
    </Grid>
      
  </Card>
);}

控制台:

在525毫秒内完成快速刷新

下卡

{} ->空数组

undergetServerSideProps

{Last1WeekQuantity: 0,Last1WeekAmount: 0,Last1MonthQuantity: 3,Last1MonthAmount: 73,Last3MonthQuantity: 3,…}

我试图获取数据并将其传递给组件。

EN

回答 1

Stack Overflow用户

发布于 2022-11-29 18:35:11

不能在非页面组件中使用getServerSideProps。应该在使用此组件的页面上使用此方法。

https://nextjs.org/docs/basic-features/data-fetching#only-allowed-in-a-page-2

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74617476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档