inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

비전공자를 위한 진짜 입문 올인원 개발 부트캠프

웹과 API 연동하기 & dayjs로 날짜 다루기

상세페이지에서 get이 안되는 경우

715

김도토리

작성한 질문수 13

1

postman으로 테스트 했을 때 product/${id} db가 잘 불러와지는데 index.js/product파일에서는 products가 null에 머물러있습니다..
import { useParams } from "react-router-dom";
import axios from "axios";
import { useEffect, useState } from "react";
import "./index.css"

function ProductPage() {
    const { id } = useParams();
    const [products, setProduct] = useState(null);
    useEffect(function () {
        axios.get(`https:/localhost:8080/products/${id}`).then(
            function (result) {
                setProduct(result.data.products);
                console.log(result);
            }
        ).catch(
            function (error) {
                console.log(error);
            }
        )
    }, [id]);

    if (products === null) {
        return <h1>값을 받아오는 중입니다...</h1>
    }

    return (
        <div>
            <div id="image-box">
                <img src={"/" + products.imageUrl} alt="product-img" />
            </div>
            <div id="profile-box">
                <img src="/images/icons/avatar.png" alt="seller-img" />
                <span>{products.seller}</span>
            </div>
            <div id="contents-box">
                <div id="name">{products.name}</div>
                <div id="price">${products.price}</div>
                <div id="create-at">생성된 날짜가 들어갈예정: 2020.1.1.</div>
                <div id="discription">{products.description}</div>
            </div>

        </div>
    );
}

export default ProductPage;

머신러닝 배워볼래요? express javascript react nodejs HTML/CSS react-native tensorflow

답변 4

1

그랩

https:localhost -> http://localhost로 변경해보시겠어요?

현재 node.js 서버는 https 프로토콜 설정이 따로 되어있지 않기 때문에 http 프로토콜로만 접근이 가능합니다 :)

 

0

김도토리

get형식으로 받아온다는 걸 방금 캐치해서 포스트맨으로 데이터를 요청했는데 데이터는 잘 받아집니당..

0

김도토리

localhost: 8080 / products / 3 에서 이런 데이터들이 보이는데 

react를 실행시켜서 localhost: 3000 / products / 3 에 접속하면 404에러가 뜨는데 왜그럴까요... 

import { useParams } from "react-router-dom";
import axios from "axios";
import { useEffect, useState } from "react";
import "./index.css"

function ProductPage() {
    const { id } = useParams();
    const [product, setProduct] = useState(null);
    useEffect(function () {
        axios.get(`http:/localhost:8080/products/${id}`).then(
            function (result) {
                setProduct(result.data.product);
                console.log(result);
            }
        ).catch(
            function (error) {
                console.log(error);
            }
        )
    }, [id]);

    if (product === null) {
        return <h1>값을 받아오는 중입니다...</h1>
    }

    return (
        <div>
            <div id="image-box">
                <img src={"/" + product.imageUrl} alt="product-img" />
            </div>
            <div id="profile-box">
                <img src="/images/icons/avatar.png" alt="seller-img" />
                <span>{product.seller}</span>
            </div>
            <div id="contents-box">
                <div id="name">{product.name}</div>
                <div id="price">${product.price}</div>
                <div id="create-at">생성된 날짜가 들어갈예정: 2020.1.1.</div>
                <div id="discription">{product.discription}</div>
            </div>

        </div>
    );
}

export default ProductPage;

0

그랩

에러 내용을 먼저 확인해보시면 좋을 것 같아요. 지금 붉은 색 쳐져있는 것 보면 제대로 된 URL이 아니죠? 

axios.get 에 http:/localhost 이렇게 슬래시/ 가 1개만 쳐져있어서 URL을 제대로 인식하지 못하고 있네요

0

김도토리

콘솔창에서는 프로토콜에러라고 뜨는데 어느부분의 문제인지 모르겠습니다...

[해결]그랩님 답변 주세요.

0

160

2

그랩님의 답변을 기다립니다/102강 전반적인 에러

0

140

2

[그랩님께]101강 안드로이드 에러들(Key prop)해결방법 궁금합니다.

0

122

2

[재질문][그랩님 답변 부탁드립니다]101강

0

135

2

[그랩님 답변 부탁드립니다]101강 Axios 에러와 502 Bad Gateway 질문

0

96

2

Ngrok 설치 후 forwarding Url 에러

0

129

2

[그랩님께,Ngrok 악성코드 인식 해결방법]질문 드립니다.

0

215

2

Ngrok 설치 후 forwarding Url로 연결 불가

0

133

1

그랩님,[꼭] 답변 부탁드립니다.

0

64

1

[꼭][[꼭] 그랩님, 답변 부탁드립니다], Failed to load resource: the server responded with a status of 404 (Not Found) 상품 상세 페이지 질문입니다.

0

147

1

6강/7강 수업

0

49

1

그랩님, 상품 상세 페이지 에러와 의문점 질문드립니다.

0

98

2

그랩님, 해결되지 않은 에러 메시지 [꼭] 답변 부탁 드립니다.

0

133

2

[재 질문]Cannot read properties of undefined (reading 'map') TypeError: Cannot read properties of undefined (reading 'map') 에러 해결 어떻게 하나요?

0

88

1

Cannot read properties of undefined (reading 'map') TypeError: Cannot read properties of undefined (reading 'map') 에러 해결 어떻게 하나요?

0

126

2

일반적인 css 꾸미기에서 width와 height의 값?

0

86

2

Windows에서의 업로드 후 홈화면 상품이미지 오류 해결방법

0

191

1

그랩마켓 웹화면 구현하기 -2 질문입니다.

0

118

1

react에 반영이 되지 않습니다.

0

233

1

터미널 npm install -g create-react-app 작성 후 오류

0

418

1

create-react-app my app 실행 시 에러

0

349

2

포스트맨 질문

0

96

1

<꼭 답변 부탁 드립니다>그랩선생님, [컴포넌트 사용하기] 강의에서 질문 있습니다.

0

256

2

그랩선생님, 질문 답변 부탁 드립니다.vscode에서 npm install -g create-react-app 입력 후 에러 입니다.

0

471

2