inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

상품 생성, 조회 API 구현

server.js와 postman post사용 시 에러

298

작성자 없음

1

안녕하세요. 강의 보면서 천천히 따라하고 있습니다. 근데 데이터베이스를 들어가면서 오류가 발생해서 어디가 잘못되었는지 모르겠습니다. 

코드를 다음과 같이 작성해서 post를 보내면 에러화면이 뜹니다.....

 

server.js 코드

const express = require('express')
const cors = require('cors')
const app = express()
const port = 8080;
const models = require('./models');

app.use(express.json())
app.use(cors())

app.get("/products", (req, res) => {
    const query = req.query;
    console.log('query : ', query)
    res.send({
        "products": [
            {
                "id": 1,
                "name": "농구공",
                "price": 100000,
                "seller": "조던",
                "imageUrl": "images/products/basketball1.jpeg"
            },
            {
                "id": 2,
                "name": "축구공",
                "price": 50000,
                "seller": "메시",
                "imageUrl": "images/products/soccerball1.jpg"
            },
            {
                "id": 3,
                "name": "키보드",
                "price": 30000,
                "seller": "Grab",
                "imageUrl": "images/products/keyboard1.jpg"
            }
        ]
    })
})

app.post("/products", (req, res) => {
    const body = req.body;
    const {name, price, seller, description,} = body;
    models.Product.create({
        name,
        description,
        price,
        seller,
    }).then((result)=>{
        console.log("상품 생성 결과: ", result);
        res.send({
            result,
        });
    }).catch((error)=>{
        console.error(error)
        res.send('상품 업로드에 문제가 발생했습니다.')
    })
});

app.get("/products/:id", (req, res) => {
    const params = req.params;
    const {id} = params;
    res.send(`id는 ${params.id}입니다.`)
})

app.listen(port, () => {
    console.log('서버가 돌아가는 중입니다.')
    models.sequelize.sync().then(()=>{
        console.log('DB연결 성공!')
    }).catch((err)=>{
        console.error(err);
        console.log('DB연결 에러...')
        process.exit();
    })
})

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

답변 1

0

그랩

현재 Product Model을 생성하는 코드를 보시면 description이 아닌 descriptions 라고 작성한 것 같아요

그래서 현재 데이터베이스의 Products 테이블에는 descriptions를 필드로 가지고 있는 것 같고, 실제 요청을 할 때는 description으로 넣어서 아래 에러 Product.descriptions cannot be null이 나오는 것 같아요!

0

jjjj

아, product model을 수정해서 server.js를 실행해도 sqlite3파일은 업데이트 되지 않아서 계속 descriptions로 필드를 가지고 있었네요ㅠㅠㅠ 감사합니다!!!!

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

0

195

2

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

0

168

2

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

0

140

2

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

0

165

2

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

0

121

2

Ngrok 설치 후 forwarding Url 에러

0

152

2

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

0

259

2

Ngrok 설치 후 forwarding Url로 연결 불가

0

159

1

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

0

78

1

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

0

163

1

6강/7강 수업

0

58

1

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

0

106

2

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

0

150

2

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

0

96

1

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

0

136

2

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

0

99

2

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

0

206

1

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

0

128

1

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

0

254

1

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

0

430

1

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

0

358

2

포스트맨 질문

0

105

1

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

0

271

2

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

0

501

2