[0509] TIL 18์ผ์ฐจ

nikevapormaxยท2022๋…„ 5์›” 9์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
17/116

๐Ÿ˜‚ [ํŒ€ํ”Œ] ์ธ์Šคํƒ€๊ทธ๋žจ ํด๋ก ์ฝ”๋”ฉ

๐Ÿ˜ญ comment API ์ƒ์„ฑ

- ์ง„ํ–‰ ๊ณผ์ •

  • ๋Œ“๊ธ€๋‹ฌ๊ธฐ์™€ ๊ด€๋ จ๋œ API ์ž‘์„ฑ์„ ์ผ๋‹จ์€ ๋งˆ์ดํŽ˜์ด์ง€์— ์žˆ๋Š” ๊ฒŒ์‹œ๊ธ€์„ ํด๋ฆญํ•˜๋ฉด ๋‚˜์˜ค๊ฒŒ ๋˜๋Š” ๋ชจ๋‹ฌ์—์„œ ์‹œ์ž‘ํ•˜์˜€๋‹ค.

  • ๋งˆ์ดํŽ˜์ด์ง€์— ์žˆ๋Š” ๊ฒŒ์‹œ๊ธ€๋“ค์„ ํด๋ฆญํ•˜๊ฒŒ ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ชจ๋‹ฌ์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.

  • ํ•˜๋‹จ์˜ ๋Œ“๊ธ€๋‹ฌ๊ธฐ์— ๋Œ“๊ธ€์„ ์ž…๋ ฅํ•œ ํ›„, ๊ฒŒ์‹œ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ๊ฒŒ์‹œ ๋ฒ„ํŠผ์— ์ ์šฉ๋˜์–ด ์žˆ๋Š” save_comment() ํ•จ์ˆ˜์— ์˜ํ•ด POST ์š”์ฒญ์ด ์ง„ํ–‰๋˜๊ณ  ๋Œ“๊ธ€์ด ๋กœ์ปฌ db์— ์ €์žฅ๋˜๊ณ  ์ €์žฅ๋œ ๋ถ€๋ถ„์„ GET ์š”์ฒญ์„ ํ†ตํ•ด ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ฒŒ ํ•˜์˜€๋‹ค.

  • js ๋ถ€๋ถ„

function save_comment() {
    const cm = $('#cm').val();

    $.ajax({
        type: 'POST',
        url: '/mypage/comment',
        data: {cm_give: cm},
        success: function (response) {

            window.location.reload();
        }
    });
}

// ์ฝ”๋ฉ˜ํŠธ ๋ถ€๋ถ„ GET ์š”์ฒญ ์ž‘์„ฑํ•ด์•ผํ•จ!๊ฒŸ ๋ฐ›์•„์™€์„œ ์‚ฌ์šฉ์žํ•œํ…Œ ๋˜ ๋ณด์—ฌ์ค˜์•ผ์ฃ 
function show_comment() {
    $.ajax({
        type: "GET",
        url: "/mypage/comment",
        data: {},
        success: function (response) {
            const comments = response['comment_info']
            console.log(comments)
            for (let i=0; i < comments.length;i++) {
                const writer = comments[i]['cm_writer']['insta_id']
                const comment = comments[i]['cm']

                const temp_html = `<div class="show_comment">
                                        <div><img style="width: 40px; border-radius: 50%"
                                                  src="https://ca.slack-edge.com/T039CS8AH0D-U03A0MUBSQH-0e4deb8911e1-512">
                                        </div>
                                        <div class="modal_pf_name">${writer}</div>
                                        <div class="modal_desc_wrtie" id="cm">${comment}</div>
                                   </div>`
                $('#comments').append(temp_html)
            }
        }
    });
}
  • flask ๋ถ€๋ถ„
@app.route("/mypage/comment", methods=["POST"])
def comment_info():
	token_receive = request.cookies.get('mytoken')
	payload = jwt.decode(token_receive, SECRET_KEY, algorithms=['HS256'])

	cm_writer_receive = db.user_info.find_one({"insta_id": payload["id"]})
	cm_receive = request.form['cm_give']

	doc = {
		'cm_writer': cm_writer_receive['insta_id'],
		'cm': cm_receive
	}

	db.comment_info.insert_one(doc)

	return jsonify({'msg': '๋Œ“๊ธ€์ž‘์„ฑ ์™„๋ฃŒ!'})


@app.route("/mypage/comment", methods=["GET"])
def comment_output():
	comment_info_list = list(db.comment_info.find({}, {'_id': False}))
	print(comment_info_list)
	return jsonify({'comment_info': comment_info_list})

- ํ•ด๋‹น ์ฝ”๋“œ์˜ ๋ฌธ์ œ์ 

  • ํ•จ์ˆ˜์˜ ์ด๋ฆ„๋“ค์„ ์ข€ ๋” ์ง๊ด€์ ์œผ๋กœ ์งฐ์–ด์•ผ ํ–ˆ๋‹ค. ์ด์ „์— ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ›์•˜์„ ๋•Œ ์ฝ”๋“œ์˜ ๊ตฌ์„ฑ์€ ๊ทธ๋ฆฌ ๊ธธ์ง€ ์•Š์•„์„œ ํŒŒ์•…ํ•˜๊ธฐ ์ข‹์œผ๋‚˜, ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์ด ๋„ˆ๋ฌด ์ค„์—ฌ์ ธ ์žˆ๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜ ์ž์ฒด๊ฐ€ ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๊ฒƒ์ธ๋ฐ ๊ตณ์ด info๋ผ๋Š” ์ด๋ฆ„์„ ๋‹ฌ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ๋‚ด์šฉ์„ ์ฃผ์…จ์—ˆ๋‹ค. ์šฐ์„ ์€ ๊ธฐ๋Šฅ ๊ตฌํ˜„์ด ์šฐ์„ ์ด๋ผ ์ƒ๊ฐํ•˜์—ฌ ํŒ€์›๋“ค๊ณผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์„๋‹ค ์‹œ๋„ํ•ด ๋ณธ ํ›„, ํ•จ์ˆ˜๋ช…์ด๋ผ๋˜์ง€ ๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ช…์„ ์ •๋ฆฌํ•˜์—ฌ ์ง€๊ธˆ๋ณด๋‹ค๋Š” ํด๋ฆฐํ•œ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.
  • ๋Œ“๊ธ€์„ ํ•˜๋‚˜ ๋‹ฌ ๋•Œ๋Š” ๋„ˆ๋ฌด ์ž˜ ๋‹ฌ๋ ค์„œ ํ–‰๋ณตํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์•„์ด๋””๊ฐ€ ์ œ๋Œ€๋กœ ๋ฐ˜์˜๋˜์ง€ ์•Š์•˜๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋„ ์ฐจ์ฐจ ์ˆ˜์ •์„ ํ•  ๊ฒƒ์ด๋ฉฐ, ๋Œ“๊ธ€์„ ์—ฌ๋Ÿฌ ๊ฐœ ๋‹ฌ๊ฒŒ ๋˜๋ฉด ๋Œ“๊ธ€์„ ์ž…๋ ฅํ•˜๋Š” ์ฐฝ์ด ๋ฐ”๋‹ฅ์„ ๋šซ๊ณ  ๋‚ด๋ ค๊ฐ„๋‹ค. ์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋„ CSS๋ฅผ ์ˆ˜์ •ํ•ด ๋ณด์™„ํ•  ๊ฒƒ์ด๋‹ค. (๋˜ํ•œ ๋‘ ๋ฒˆ์งธ ์ง์ ‘ ์ž‘์„ฑํ•œ ๋Œ“๊ธ€๋ถ€ํ„ฐ ๋Œ“๊ธ€์˜ ๋‚ด์šฉ์ด ๋‚˜์˜ค์ง€ ์•Š๋Š”๋‹ค.)

- ํ•ด๊ฒฐ ๊ณผ์ •

  • ์šฐ์„  ๋Œ“๊ธ€์ฐฝ์ด ์•„๋ž˜๋กœ ๋šซ๊ณ  ๋‚ด๋ ค๊ฐ€๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์ค‘๊ฐ„์˜ ๋Œ“๊ธ€์ด ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์˜€๋‹ค.

  • ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋‹ˆ ๋Œ“๊ธ€ ์ž…๋ ฅ์นธ์€ ๋‚ด๋ ค๊ฐ€์ง€ ์•Š์•˜์ง€๋งŒ ๋Œ“๊ธ€์„ ์นธ์ด ์ˆ˜์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ด์ƒ์˜ ์–‘์„ ๋‹ฌ๊ฒŒ ๋˜๋‹ˆ ๋Œ“๊ธ€ ์ž…๋ ฅ์นธ์„ ์นจ๋ฒ”ํ•˜๊ธฐ ์‹œ์ž‘ํ•˜์˜€๋‹ค.

  • ๋”ฐ๋ผ์„œ ๋Œ“๊ธ€์ฐฝ์— ์Šคํฌ๋กค์„ ๋‹ฌ๊ณ ์ž ํ•˜์˜€๊ณ , ๊ตฌ๊ธ€๋ง์„ ํ†ตํ•ด ํŠน์ • div์— ์Šคํฌ๋กค ๋‹ค๋Š” ๋ฒ•์„ ์ฐพ์•„ ์Šคํฌ๋กค์„ ๋‹ซ์•˜๋‹ค.

  • ๋‹ซ๊ณ ๋‚˜๋‹ˆ ๋˜ ์š•์‹ฌ์ด ์ƒ๊ฒจ ์ธ์Šคํƒ€๋ฅผ ์ด๋ฆฌ์ €๋ฆฌ ๋œฏ์–ด๋ณด๋‹ˆ ์ธ์Šคํƒ€์˜ ๋Œ“๊ธ€์ฐฝ์—๋Š” ๊ธ€์Ž„ ์Šคํฌ๋กค์ด ์—†์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋˜ ์Šคํฌ๋กค์„ ์ˆจ๊ธฐ๋Š” ๋ฒ•์„ ๊ตฌ๊ธ€๋งํ•˜์˜€๋‹ค.

  • ํ•ด๋‹น ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๊ณ  ๋‚˜๋‹ˆ ์•„์ฃผ ์ด์œ ๋Œ“๊ธ€์ฐฝ์ด ์ƒ๊ธฐ๊ฒŒ ๋˜์—ˆ๋‹ค.

  • ๋Œ“๊ธ€ ์ž‘์„ฑ์ž๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๋ถ€๋ถ„๋„ ํŒ€์›๋“ค๊ณผ ์ž์„ธํžˆ ์ฝ”๋“œ๋ฅผ ๊ด€์ฐฐํ•˜๋‹ค๋ณด๋‹ˆ ์ž‘์„ฑ์ž์˜ ์ด๋ฆ„์„ ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์—์„œ db๋ณด๋‹ค ๊นŠ์€ ๊นŠ์ด์˜ ์ด๋ฆ„์„ ๊บผ๋‚ด์˜ค๋ผ๊ณ  ๋ช…๋ น์„ ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด db์— ๋‚˜์™€์žˆ๋Š” ๋ถ€๋ถ„๊นŒ์ง€๋งŒ์— ๋Œ€ํ•ด ๋ช…๋ น์„ ๋‚ด๋ ธ๊ณ  ํ•ด๊ฒฐ์ด ๋˜์—ˆ๋‹ค. (์•„๋ž˜์˜ name ๋ถ€๋ถ„)

success: function (response) {
            const prof_name = response['user_info_list']
            const name = prof_name[0]['name']
            const insta_id = prof_name[0]['insta_id']
  • ๋Œ“๊ธ€์˜ ๋‚ด์šฉ์ด ๋‚˜์˜ค์ง€ ์•Š๋Š” ๋ฌธ์ œ์ ์€ id๊ฐ’์„ ์ด์ƒํ•œ๋ฐ๋‹ค ์ฃผ๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ์–ด๋‚œ ๊ฒƒ์ด์—ˆ๋‹ค. ๋Œ“๊ธ€์„ ๋‹ค๋Š” input์ฐฝ์— id๋ฅผ ์ž˜ ๋ถ€์—ฌํ•ด ๋†“๊ณ , javascript๋กœ ๋ฐ›์•„์˜ค๋Š” ๋ถ€๋ถ„์— ๋˜‘๊ฐ™์€ id ๊ฐ’์„ ์ฃผ๊ณ  ์žˆ์—ˆ๋‹ค. ๋”ฐ๋ผ์„œ javascript ์ชฝ์˜ id๋ฅผ ์‚ญ์ œํ•ด ์ฃผ์—ˆ๊ณ , ๊ฐ’์ด ์ž˜ ๋‚˜์˜ค๊ฒŒ ๋˜์—ˆ๋‹ค. (์œ— ์‚ฌ์ง„ ์ฐธ๊ณ )

๐Ÿ˜ญ signup js ์ˆ˜์ •

  • ์›๋ž˜ ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ฉด ๋กœ๊ทธ์ธ ์ฐฝ์œผ๋กœ ๋„˜์–ด๊ฐ€๊ฒŒ ๋˜์–ด ์žˆ์–ด์•ผ ํ•˜๊ณ , ํšŒ์›๊ฐ€์ž…์— ์‹คํŒจํ•˜๊ฒŒ ๋˜๋ฉด ํšŒ์›๊ฐ€์ž… ์ฐฝ์—์„œ ์ƒˆ๋กœ๊ณ ์นจ์ด ๋˜์–ด ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋„๋ก ์‚ฌ์šฉ์ž๋ฅผ ์œ ๋„ํ•ด์•ผ ํ•œ๋‹ค.
  • ์˜ค๋Š˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ํ›„ ํŒ€์›๋“ค๊ณผ ์‹œ์—ฐ์„ ํ•˜๋Š” ๊ณผ์ •์—์„œ ํ•ด๋‹น ํŽ˜์ด์ง€์˜ ๊ฒฝ๋กœ ์˜ค๋ฅ˜๋ฅผ ์ฐพ์•„๋‚ด๊ฒŒ ๋˜์–ด ์ˆ˜์ •ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.
  • ์ฒซ ๋ฒˆ์งธ ์‹œ๋„
    • ์ฒซ ๋ฒˆ์งธ์—๋Š” if ์กฐ๊ฑด๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค ํ–ˆ๋‹ค. ์ƒ๊ฐ๋งŒํ•ด๋„ ๋„ˆ๋ฌด ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์ด ๋ช…ํ™•ํ•ด ์‹ ์ด ๋‚˜์„œ ์ฝ”๋”ฉ์„ ํ–ˆ๋‹ค.
    • ํ•˜์ง€๋งŒ ์ˆ˜์ • ํ›„ ์‹œ์—ฐ์„ ํ•˜๋Š” ์ˆœ๊ฐ„, ๊ฒฐ๊ณผ๊ฐ€ ์ด์ „๊ณผ ๋‹ค๋ฅผ๋ฐ”๊ฐ€ ์—†์—ˆ๋‹ค.
    • ๋กœ๊ทธ์ธ ์„ฑ๊ณต -> ํšŒ์›๊ฐ€์ž… ์ฐฝ
      ๋กœ๊ทธ์ธ ์‹คํŒจ -> ํšŒ์›๊ฐ€์ž… ์ฐฝ
function save_user_info() {
    const contact = $('#contact').val();
    const name = $('#name').val();
    const insta_id = $('#user_id').val();
    const password = $('#password').val();

    $.ajax({
        type: 'POST',
        url: '/signup',
        data: {contact_give: contact, name_give: name, insta_id_give: insta_id, password_give: password},
        success: function (response) {
            if (response["result"] == "success") { 
                alert(response['msg'])
                window.location.href = '/login';
            } else {
                alert(response['msg'])
                window.location.reload();
            }
        }
    });
}
  • ๋‘ ๋ฒˆ์งธ ์‹œ๋„
    • ๋‚ด๊ฐ€ ์ƒ๊ฐํ•œ ๋ถ€๋ถ„์ด ๋„ˆ๋ฌด๋‚˜ ๋‹น์—ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ๋ชป ๋‚ด๊ณ , ํŠœํ„ฐ๋‹˜๊ป˜ ์งˆ๋ฌธ์„ ๋“œ๋ ธ๋‹ค.
    • ์ด๊ณณ์ €๊ณณ์— print()์™€ console.log()๋ฅผ ํ•˜๋ฉฐ ์˜ค๋ฅ˜๋ฅผ ์ถ”์ ํ•˜๋‹ค๊ฐ€ sign-up.html์„ ํ™•์ธํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ํ•ด๋‹น ๋ถ€๋ถ„์˜ jasonify ๋ถ€๋ถ„์— response["result"] == "success"์—์„œ ์‚ฌ์šฉํ•œ result ๊ฐ’์„ ์„ ์–ธํ•ด์ฃผ์ง€ ์•Š์•˜์—ˆ๋˜ ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
    • ํ•ด๋‹น ๋ถ€๋ถ„์— ๋Œ€ํ•œ ์„ ์–ธ์„ ๋งˆ์น˜๊ณ  ๋‚˜๋‹ˆ ์ƒ๊ฐํ–ˆ๋˜ ๋ฐ๋กœ ๊ฒฝ๋กœ๊ฐ€ ์ž˜ ์—ฐ๊ฒฐ๋˜์—ˆ๋‹ค.
      ๋กœ๊ทธ์ธ ์„ฑ๊ณต -> ๋กœ๊ทธ์ธ ์ฐฝ
      ๋กœ๊ทธ์ธ ์‹คํŒจ -> ํšŒ์›๊ฐ€์ž… ์ฐฝ
return jsonify({'result': 'success', 'msg': 'ํšŒ์›๊ฐ€์ž… ์™„๋ฃŒ!'})
profile
https://github.com/nikevapormax

0๊ฐœ์˜ ๋Œ“๊ธ€