• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

선생님! product 테이블에 컬럼이 일부 저장이 안됩니다

21.10.25 02:09 작성 조회수 108

0

선생님! 강의 너무 유용해요 잘 듣고있습니다~

그런데 마지막 부분에서 디비에 저장되는 컬럼에 body에서 보내준 내용 일부분만 저장이 되고 있습니다.

로그를 찍어봐도 body에는 정상적으로 잘 들어갔는데 오류 메세지도 없고.. 왜 이럴까요? 제가 누락한 부분이 있을까요..??

>client/UploadProductPage.js

const submitHandler = e => {
console.log("upload to DB");
e.preventDefault();
if (!TitleValue || !Description || !Price || !Continent || !Images) {
return alert("모든 값을 넣어주세요.");
}
const body = {
//로그인 된 사람의 ID를 넣어줘야 한다.
writer: props.user.userData._id,
title: TitleValue,
description: Description,
price: Price,
images: Images,
continents: Continent
};
Axios.post("/api/product", body).then(res => {
if (res.data.success) {
alert("상품 업로드에 성공했습니다.");
console.log(body);
props.history.push("/");
} else {
alert("상품 업로드에 실패했습니다.");
}
});
};

>server/model/Product.js

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const productSchema = mongoose.Schema(
{
writer: {
type: Schema.Types.ObjectId,
ref: "User"
},
title: {
type: String,
maxlength: 50
},
description: {
type: String
},
price: {
type: Number,
default: 0
},
images: {
type: Array,
default: []
},
sold: {
type: Number,
maxlength: 100,
default: 0
},

continents: {
type: Number,
default: 1
},

views: {
type: Number,
default: 0
}
},
{ timestamps: true }
);

productSchema.index(
{
title: "text",
description: "text"
},
{
weights: {
title: 5,
description: 1
}
}
);
const Product = mongoose.model("Product", productSchema);

module.exports = { Product };

>console.log 결과

>mongo DB 저장 결과

 

 

 

바쁘시겠지만 알려주시면 감사하겠습니다!

 

답변 2

·

답변을 작성해보세요.

1

혀니님의 프로필

혀니

질문자

2021.10.29

선생님!  req.body가 server에서는 String 값만 undefined로 나와서, 인코딩 문제였던 것 같아요~ body-parser 랑 인코딩 설정 해주고 나서 정상적으로 저장되었습니다 ㅎㅎ 도움 주셔서 감사합니다!

호박인절미님의 프로필

호박인절미

2022.08.05

저도 같은 문제인데 ㅠㅜ  혹시 인코딩 설정 어떻게 하셨나 공유해주실 수 있나요? 

0

안녕하세요 혀니님 !!  아마   백엔드에서 저장하는 부분을 봐야할 것 같은데 
그부분을 올려주시겠어요?!!! 새로운 게시물에 그 부분이랑 전체 저장소 주소를 올려주시면 직접봐보겠습니다 !!