node 2022/02/17

무간·2022년 2월 17일

sqlbooster에서 수행

db.sequence.insert({ _id : 'SEQ_CART1_NO', seq : 500, })

==============================================

파일명 : shop.js

// 장바구니 가져가기
// localhost:3000/shop/selectcart
router.get('/selectcart', async function(req, res, next){
    try{        
        const dbconn = await db.connect(dburl);
        const userid = req.headers['x-forwarded-for'];
        // cart1에서 현재 접속한 구매자 ip정보에 해당하는 목록 받기
        const collection = dbconn.db(dbname).collection('cart1');            
        const result = await collection.find(
            { userid : userid }
        ).toArray();
        console.log(result);        
        // cart1 + item1에 있는 정보가져오기
        const collection1 = dbconn.db(dbname).collection('item1');
        // 받은 목옥에 물품번호를 꺼내서 item1에 정보를 가져와 합치기
        for(let i=0; i<result.length; i++){        
            const result1 = await collection1.findOne(
                { _id : result[i].code },
                { projection : { price:1, name:1 } }
            )
            // 합치는 부분
            result[i].itemname = result1.name;
            result[i].itemprice = result1.price;
        }
        return res.send({status:200, result:result});    
        
    }
    catch(e){
        console.error(e);
        return res.send({status : -1, message:e });
    }
});


// 장바구니
// localhost:3000/shop/insertcart
router.post('/insertcart', async function(req, res, next){
    try{
        console.log(req.headers['x-forwarded-for']);
        console.log(req.body);
        const dbconn = await db.connect(dburl);
        const collection = dbconn.db(dbname).collection('sequence');
        const result = await collection.findOneAndUpdate(
            { _id  : 'SEQ_CART1_NO' }, // sequence 값( _id 자동 생성)
            { $inc : {seq : 1} }        // 1씩 증가
        );    

        const obj = {
            // 구매자가 접속한PC의 ip주소를 사용
            _id    :result.value.seq,
            userid : req.headers['x-forwarded-for'],
            code   : Number(req.body.code),
            cnt    : Number(req.body.cnt),
        }
        // DB에 추가하기        
        // DB선택 및 컬렉션 선택
        const collection1 = dbconn.db(dbname).collection('cart1');    
        const result1 = await collection1.insertOne(obj);
        console.log(result1);
        if(result1.insertedId === obj._id) {
            return res.send({status:200});    
        }
        return res.send({status:0});
    }
    catch(e){
        console.error(e);
        return res.send({status : -1, message:e });
    }
});

==============================================

파일명 /src/ItemCart.vue

<template>
    <div>
        <h3>/src/ItemCart.vue</h3>
         <table border="1">
            <thead>
                <tr>
                    <th>이름</th>
                    <th>가격</th>
                </tr>                
            </thead>
            <tbody>
                <tr v-for="item in state.items" :key="item">                    
                    <td>{{item.itemname}}</td>
                    <td>{{item.itemprice}}</td>
                </tr>
            </tbody>
        </table>        

    </div>
</template>

<script>
import { onMounted, reactive } from 'vue'
import { useRoute } from 'vue-router';
import axios from 'axios';

export default {
    setup () {
        const route = useRoute();        

        const state = reactive({
            code : route.query.code,
        });


        const handleLoadData = async() => {
            const url = `/shop/selectcart`;
            const headers = {"Content-Type":"application/json"}
            // 물품번호, 수량, 로그인하지 않은 사용자            
            const response = await axios.get(url, {headers});
            console.log(response.data);
            if(response.data.status === 200 ){
                state.items = response.data.result;
            }

        }
         onMounted( async()=>{
            handleLoadData();
        });
        

        return {state}
    }
}
</script>

<style lang="scss" scoped>

</style>
profile
당신을 한 줄로 소개해보세요

0개의 댓글