티스토리 뷰
Project/첫번째 프로젝트 쇼핑몰 웹
[node js - express 쇼핑몰 웹] 2. 삭제된 product 정보 cart와 order에 적용시키기
무니웜테일패드풋프롱스 2020. 7. 15. 00:25📌2020 07 12
-
UI 수정
-
사용자가 특정 아이템 스키마를 삭제했는데 Cart와 Order에 해당 아이템이 담겨 있는 버그 발견. null로 인식하여 getCart 라우팅과 getOrder 라우팅에서 에러 발생
-
각각 User 모델과 Order 모델에서 renew 메서드 구현. 현재 Cart와 Order에 저장된 ProductId에 해당하는 모든 Product를 찾고, 해당 Product의 개수와 현재 Cart, Order에 담겨있는 아이템의 개수가 다를 경우 Cart와 Order를 갱신해주도록 함
renew메서드
OrderSchema.methods.renewOrder=function(){
const productIds = this.products.items.map(i=>{
return i.productId;
});
const updatedOrderItems=[];
return Product.find({'_id':productIds})
.then(products=>{
if(Object.keys(products).length < Object.keys(this.products.items).length){ // length가 다르다면
for( p of products){
const qtity = this.products.items.find(i=>{
return i.productId.toString()===p._id.toString(); // id값이 같다면
}).quantity; // quantity 추출
if(qtity>0) updatedOrderItems.push({productId:p._id, quantity: qtity}); // quantity가 추출되었다면
}
}
this.products.items=updatedOrderItems;
return this.save();
})
'Project > 첫번째 프로젝트 쇼핑몰 웹' 카테고리의 다른 글
[ node js - express 쇼핑몰 웹] 6. Cart 와 Order 수량 변경 기능 구현 / Cart 내에서 체크한 항목만 Order하는 기능 구현 (0) | 2020.07.22 |
---|---|
[ node js - express 쇼핑몰 웹] 5. Seller 등록, 취소 구현 (0) | 2020.07.22 |
[ node js - express 쇼핑몰 웹 ] 4. input data validation 추가 (0) | 2020.07.19 |
[node js - express 쇼핑몰 웹] 3. 로그인, 로그아웃, 회원가입 구현하기 (0) | 2020.07.16 |
[node js - express 쇼핑몰 웹] 1. Mongo DB 설정 (0) | 2020.07.15 |
댓글