简体   繁体   English

来自我的 API 的 Material UI CardMedia Image 数据

[英]Material UI CardMedia Image data coming from my API

Has anyone of you experienced this error when using the CardMedia API of Material-UI?有没有人在使用 Material-UI 的 CardMedia API 时遇到过这个错误?

Currently i'm using the Card & CardMedia from material-ui to render information coming from my api, but I'm having issue displaying the Image since I'm pulling the image on ComponentDidMount seems like the image should already be available before then.目前,我正在使用 material-ui 中的 Card & CardMedia 来呈现来自我的 api 的信息,但是我在显示图像时遇到了问题,因为我在 ComponentDidMount 上拉动图像似乎在此之前图像应该已经可用。

This is the error这是错误

Warning: Failed prop type: Material-UI: Either `children`, `image`, `src` or `component` prop must be specified.

This is the code for the mother component这是母组件的代码

async componentDidMount() {
    try {
      const getScreams = await axios.get("/screams");
      this.setState({ screams: getScreams.data });
    } catch (error) {
        console.log(error)
    }
  }
  render() {
     let recentScreamsMarkup =  this.state.screams ? (
        this.state.screams.map(item => <Scream key={item.screamId} scream={item}/>)
     ) : <p>Loading ...</p>
    return (
      <Grid container spacing={8}>
        <Grid item sm={8} xs={12}>
          {recentScreamsMarkup}
        </Grid>
        <Grid item sm={4} xs={12}>
          <p>Profile ...</p>
        </Grid>
      </Grid>
    );
  }
}

and for each of those I have this custom component对于每一个我都有这个自定义组件

export class Scream extends Component {

    render() {
        const { classes, scream : {body, commentCount, createdAt, likecount, userHandle, userImage, screamId } } = this.props;
        return (
            <Card className={classes.card}>
                <CardMedia className = {classes.image}
                    image={userImage}
                    title={"Profile Image"}
                />
                <CardContent className={classes.content}>
                    <Typography variant="h5" component={Link} to={`/users/${userHandle}`} color= "primary">
                        {userHandle}
                    </Typography>
                    <Typography variant="body2"
                    color='textSecondary'>
                    {createdAt}
                    </Typography>
                    <Typography variant="body1">
                        {body}
                    </Typography>
                </CardContent>

            </Card>
            
        )
    }
}

I know that the code is working as expected since I tried to put a static img url and it is pulling that data with no problem.我知道代码按预期工作,因为我尝试放置静态 img url 并且它可以毫无问题地提取该数据。

all data is showing in the card except for the image.除图像外,所有数据都显示在卡片中。

this is the default state from the Home component:这是 Home 组件的默认状态:

constructor(props) {
    super(props);
    // Don't call this.setState() here!
    this.state = {
      screams: null,
    };
  }

用户界面图片

According to card media api it is written this:根据card media api,它是这样写的:

Image to be displayed as a background image.要显示为背景图像的图像。 Either image or src prop must be specified.必须指定 image 或 src prop。 Note that caller must specify height otherwise the image will not be visible .请注意,调用者必须指定高度,否则图像将不可见

So in your card image css try this:所以在你的卡片图像 css 中试试这个:

image: {
    height: 140,
  }

That will resolve your issue if your image path is correct(can change height according to your own requirement).如果您的图像路径正确,这将解决您的问题(可以根据您自己的要求更改高度)。

Sorry for all the trouble, my silly ask did not even tried to console log the returned data from my API, that being said I've tried doing just that and found out that I am not including the userImage in the returned data.很抱歉所有的麻烦,我愚蠢的问甚至没有尝试控制台记录从我的 API 返回的数据,据说我已经尝试这样做并发现我没有在返回的数据中包含 userImage 。

I updated by back-end code and this is now working as intended.我通过后端代码更新,现在按预期工作。

Thank you all for the suggestions!谢谢大家的建议!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM