未处理的拒绝(TypeError):无法读取未定义的属性“ image”

时间:2019-11-08 08:02:30

标签: javascript reactjs

我的React应用程序有问题。首先,键入 yarn start 后,它运行良好。但是,我故意从本地json服务器断开连接,以查看生成了什么错误。预计会看到类似这样的东西。

Expected response

相反,我在浏览器的控制台中看到了这一点。

Unhandled Rejection (TypeError): Cannot read property 'image' of undefined
RenderCard
src/components/HomeComponent.js:28
  25 | return(
  26 |     <Card>
  27 |         <CardBody>
> 28 |         <CardImg src={baseUrl + item.image} alt={item.name} />
     | ^  29 |             <CardTitle>{item.name}</CardTitle>
  30 |             {item.designation ? <CardSubtitle>{item.designation}</CardSubtitle>: null}
  31 |             <CardText>{item.description}</CardText>
View compiled
▶ 23 stack frames were collapsed.
(anonymous function)
src/redux/ActionCreators.js:31
  28 |     error => {
  29 |         var errorMessage = new Error(error.errorMessage);
  30 |         throw errorMessage;
> 31 |     }
     | ^  32 | )
  33 | .then(response => response.json())
  34 | .then(dishes => dispatch(addDishes(dishes)))

这是我定义在减速器上发送的动作的方式。

import * as ActionTypes from './ActionTypes';
import { baseUrl } from '../shared/baseUrl';

export const addComment = (dishId, rating, author, comment) => ({
    type: ActionTypes.ADD_COMMENT,
    payload: {
        dishId: dishId,
        rating: rating,
        author: author,
        comment: comment
    }
});

export const fetchDishes = () => (dispatch) => {
    dispatch(dishesLoading(true));

    return fetch(baseUrl + 'dishes')
        .then(response => {
            if (response.ok) {
                return response;
            } else {
                var error = new Error('Error ' + response.status + ': ' + response.statusText);
                error.response = response;
                throw error;
            }
        },
            error => {
                var errorMessage = new Error(error.errorMessage);
                throw errorMessage;
            }
        )
        .then(response => response.json())
        .then(dishes => dispatch(addDishes(dishes)))
        .catch(error => dispatch(dishesFailed(error.message)))
}

export const dishesLoading = () => (dispatch) => ({
    type: ActionTypes.DISHES_LOADING
});

export const dishesFailed = (errmess) => ({
    type: ActionTypes.DISHES_FAILED,
    payload: errmess
});

export const addDishes = (dishes) => ({
    type: ActionTypes.ADD_DISHES,
    payload: dishes
});

export const fetchComments = () => (dispatch) => {
    return fetch(baseUrl + 'comments')
        .then(response => {
            if (response.ok) {
                return response;
            } else {
                var error = new Error('Error ' + response.status + ': ' + response.statusText);
                error.response = response;

                throw error;
            }
        },
            error => {
                var errorMessage = new Error(error.errorMessage);
                throw errorMessage;
            }
        )

        .then(response => response.json())
        .then(comments => dispatch(addComments(comments)))
        .catch(error => dispatch(commentsFailed(error.message)))
}

export const commentsFailed = (errmess) => ({
    type: ActionTypes.COMMENTS_FAILED,
    payload: errmess
});

export const addComments = (comments) => ({
    type: ActionTypes.ADD_COMMENTS,
    payload: comments
});

export const fetchPromos = () => (dispatch) => {
    dispatch(promosLoading(true));

    return fetch(baseUrl + 'promotions')
        .then(response => {
            if (response.ok) {
                return response;
            } else {
                var error = new Error('Error ' + response.status + ': ' + response.statusText);
                error.response = response;

                throw error;
            }
        },
            error => {
                var errorMessage = new Error(error.errorMessage);
                throw errorMessage;
            }
        )
        .then(response => response.json())
        .then(promos => dispatch(addPromos(promos)))
        .catch(error => dispatch(promosFailed(error.message)))
}

export const promosLoading = () => (dispatch) => ({
    type: ActionTypes.PROMOS_LOADING
});

export const promosFailed = (errmess) => ({
    type: ActionTypes.PROMOS_FAILED,
    payload: errmess
});

export const addPromos = (promos) => ({
    type: ActionTypes.ADD_PROMOS,
    payload: promos
});

还有我的HomeComponent

function RenderCard({item,isLoading,errMess}){ 
    if (isLoading){
        return(
            <div className="container">
                <div className="row">
                    <Loading />
                </div>
            </div>
        );
    }
    if(errMess){
        return(
            <div className="container">
                <div className="row">
                    <h4>{errMess}</h4>
                </div>
            </div>
        );
    } else
        return(
            <Card>
                <CardBody>
                <CardImg src={baseUrl + item.image} alt={item.name} />
                    <CardTitle>{item.name}</CardTitle>
                    {item.designation ? <CardSubtitle>{item.designation}</CardSubtitle>: null}
                    <CardText>{item.description}</CardText>
                </CardBody>
            </Card>
        );
}

function Home(props){
    return(
        <div className="container">
            <div className="container">
                <div className="row align-items-start">
                    <div className="col-12 col-md-4 m-1">
                        <RenderCard item={props.dish} 
                            isLoading={props.dishesLoading} 
                            errMess={props.dishesErrMess} 
                        />
                    </div>
                    <div className="col-12 col-md m-1">
                    <RenderCard item={props.promotion} isLoading={props.promoLoading} errMess={props.promoErrMess} />
                    </div>
                    <div className="col-12 col-md m-1">
                        <RenderCard item={props.leader} />
                    </div>
                </div>
            </div>
        </div>
    );
}

export default Home;

这是我的项目

https://github.com/theo82/Front-End-Web-Development-With-React

如何解决该错误? 谢谢, 西奥。

3 个答案:

答案 0 :(得分:1)

{p}组件中的errMess

正确道具是RenderCard而不是props.dishErrMess。 确实在MainComponent props.dishesErrMess

答案 1 :(得分:0)

如我所见,您正在过滤领导者并在MainComponent.js第53行中传递第一个索引,该索引可能始终是未定义的。您应该检查该值是否存在。

为此,将HomeComponent.js第25行替换为以下代码:

 return (item?<Card>
                <CardBody>
                <CardImg src={baseUrl + item.image} alt={item.name} />
                    <CardTitle>{item.name}</CardTitle>
                    {item.designation ? <CardSubtitle>{item.designation}</CardSubtitle>: null}
                    <CardText>{item.description}</CardText>
                </CardBody>
            </Card>:null)

此外,您可以呈现一个占位符组件,而不是我编写的null值。那取决于你。

答案 2 :(得分:0)

这属于Coursera的React Course全栈开发。

  • 启动MongoDB服务器。
  • 然后启动在服务器端开发中实现的REST API服务器 使用NodeJS,Express和MongoDB。
  • 然后开始ConFusion-React(必须 通过npm start或yarn start包含所有相关的节点模块。

如果此错误仍然存​​在,请进行以下更改:

  • 启动REST API服务器和MongoDB服务器

  • 打开邮递员并以admin登录

  • 然后使用从资源下载的db.json文件并插入所有 各个端点的菜,领导者,促销和用户,并在 通过使用不同的命令访问MongoDB Shell。

  • 然后完成我上面讨论的所有初始过程。

进行Upvote如果这可以解决您的问题,请发表评论。