• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

Cannot read property 'path' of undefined

20.06.24 01:59 작성 조회수 796

0

안녕하세요. 유튜브에서 강의 듣다가 이곳으로 오게 됐네요. 좋은 강의 감사드립니다.

다름이 아니라 이미지를 업로드하고 submit 버튼을 누르면 landing page에 이미지가 나오지 않는 에러에서 막혔습니다. 이미지를 업로드 하고 나면 object  콘솔에 이미지 주소가 아니라 empty array로 나오는 것으로 보아 뭔가 잘못된 거 같은데 무엇인지를 모르겠습니다. (postman도 empty array로 나옵니다.)

아, 그리고 submit을 눌렀을 때 uploads 폴더에 이미지가 바로 안 뜨고 vscode를 껐다 켜면 그때야 생깁니다. 

postman 에서 post 해보니 "Cannot read property 'path' of undefined"라는 에러가 떴습니다! 

깃헙 : https://github.com/dongha1992/MERN-boilerplate

답변 2

·

답변을 작성해보세요.

1

안녕하세요 Dongha님 ! 열심히하시는 모습 보기 좋네요 ^^ 

우선  

1.

product.save((err) => {
if (err) return status(400).json({
success: false,

여기 부분 

if (err) return res.status(400).json({

이렇게 res 추가해주시고요 ~  

2.


product.save((err) => {
if (err) return res.status(400).json({
success: false,
err
})
return res.status(200).json({
success: true
})
})
Product(req.body)

여기에 마지막 

Product(req.body) 지워주시구요 ~ 

3. 마지막으로 

UploadProductPage 에 오시면 

마지막 onSubmit 하실떄 

const variables = {
writer: props.user.userData._id,
title: TitleValue,
description: DescriptionValue,
price: PriceValue,
image: Images,
continents: ContinentValue,
};

이거를 


const variables = {
writer: props.user.userData._id,
title: TitleValue,
description: DescriptionValue,
price: PriceValue,
images: Images,
continents: ContinentValue,
};

이렇게 바꿔주세요 !  

차이는 image 와 images 입니다 

images가 되어야 하는 이유는 Model 에 스키마짤때 images라고 정해줬지 때문입니다 ~ ! 

price: {
type: Number,
default: 0
},
images: {
type: Array,
default: []
},

수고하세요 ^^ 

0

LandinPage.js 

  const renderCards = Products.map((product, index) => {
    return (
      <Col key={index} lg={6} md={8} xs={24}>
        <Card cover={<img src={`http://localhost:5000/${product.images[0]}`}/>}>
          <Meta title={product.title} description={`$${product.price}`} />
        </Card>
      </Col>
    );
  });

이 부분을 이렇게 수정하니까 LandingPage에 이미지가 뜨는데 여전히 주소를 찾지 못하고 콘솔에는  /undefined 라고 나옵니다!