ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ



์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ–๋Š” ํ…Œ์ด๋ธ”์„ ์ •์˜ํ•˜๋Š” ์ด์œ ๋Š” ํ•ด๋‹น ํ‚ค ๊ฐ’์„ ํ†ตํ•ด ์ฐธ์กฐํ•˜๋Š” ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๋ฅผ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•จ์ด์ฃ . ์ด๋Ÿฐ ์ƒํ™ฉ์„ ๊ฐ€์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.



๊ฐ ํ…Œ์ด๋ธ”์˜ id๊ฐ’์€ index ์—ญํ• ์„ ํ•˜๋Š” ๊ธฐ๋ณธ ํ‚ค ์ด๊ณ , board ํ…Œ์ด๋ธ”์˜ fk_userId ํ•„๋“œ๋Š” userํ…Œ์ด๋ธ”์˜ ์™ธ๋ž˜ํ‚ค์ž…๋‹ˆ๋‹ค. ์ฆ‰ fk_userId์˜ ๊ฐ’์„ ํ†ตํ•ด์„œ user ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์–˜๊ธฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


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


๊ทผ๋ฐ ์ด ๊ธ€์„ ์“ด ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฐ์ง€๋ฅผ ์•Œ๊ณ ์‹ถ์œผ๋ฉด user ํ…Œ์ด๋ธ”์—์„œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์™€์•ผํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฒŒ์‹œํŒ ๊ธ€์„ ๋กœ๋”ฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function boardLoadFunction(callback) {
    models.Board.findAll().then(function(result){    //result๊ฐ€ board ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๊ฐ’์„ json์œผ๋กœ ๋ฐ˜ํ™˜
        var jsonObj = [];                //board ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด
        var tasks = [
            function(call){            
                if(result.length==0)    //๊ฒŒ์‹œํŒ์— ๊ธ€์ด ํ•˜๋‚˜๋„ ์—†์„ ๋•Œ0๊ฐ’ ๋ฆฌํ„ด
                    callback(0)
                else{
                    for(var i=0;i<result.length;i++)        //๋ฐ์ดํ„ฐ์˜ ๊ฐœ์ˆ˜๋งŒํผ ๋ฐ˜๋ณต๋ฌธ
                        jsonObj.push(result[i].dataValues)    //jsonObj ๋ฐฐ์—ด์— select * from board ๊ฐ’์„ ์ €์žฅ
                    call(null,jsonObj);                        //์œ„์˜ ์ฟผ๋ฆฌ๋ฌธ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์Œ ํ•จ์ˆ˜๋กœ ๋„˜๊ฒจ์คŒ
                }
            },
            function(jsonObj,call){
                var i =0;                            
                var nameList = {};                            //{ ์™ธ๋ž˜ํ‚ค : ์ด๋ฆ„ } ์˜ ํ˜•ํƒœ๋กœ ๋งŒ๋“ค json ๋ฐ์ดํ„ฐ
                jsonObj.forEach(function(element){            //board ํ…Œ์ด๋ธ”๊ณผ userํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋น„๊ต
                    models.User.findOne({                    
                        where:{id:element.fk_userId}        //board์˜ ์™ธ๋ž˜ํ‚ค์™€ user์˜ id๊ฐ’์˜ ์ผ์น˜์—ฌ๋ถ€
                    }).then(function(user){
                        nameList[element.fk_userId] = user.dataValues.name    
                        //nameList ๊ฐ์ฒด์— { ์™ธ๋ž˜ํ‚ค : ์ด๋ฆ„ } ์œผ๋กœ ์ €์žฅ
                        i++;
                        if(jsonObj.length==i)    //๋ฐฐ์—ด ๋๊นŒ์ง€ ๋น„๊ต๊ฐ€ ๋๋‚˜๋ฉด ๋‹ค์Œ ํ•จ์ˆ˜๋กœ ๋„˜๊ฒจ์คŒ
                            call(null,nameList);        
                    })
                })
            },
            function(nameList,call) {            //jsonObj์— name๊ฐ’์„ ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ตœ์ข…์ ์œผ๋กœ ๋งŒ๋“ฆ
                var i=0
                jsonObj.forEach(function(element){    
                    if(nameList.hasOwnProperty(element.fk_userId)){
                        jsonObj[i].name = nameList[element.fk_userId]    //jsonObj๊ฐ์ฒด์— name key์™€ key value ์ถ”๊ฐ€
                        i++
                    }
                    if(i==jsonObj.length)
                        callback(jsonObj)        //๋ฐฐ์—ด ๋๊นŒ์ง€ ๋น„๊ต๊ฐ€ ๋๋‚˜๋ฉด app.js๋กœ ๋„˜๊ฒจ์คŒ
                })
            }
        ]
        async.waterfall(tasks,function(err){
            if(err)
                console.log(err)
        })
    }).catch(function(err){
        console.log(err);
    })
}
cs


async ๋ชจ๋“ˆ์„ ์ด์šฉ ํ•˜์˜€๋Š”๋ฐ, ์ผ๋‹จ ๊ฒŒ์‹œํŒ์— ๊ธ€์ด ํ•˜๋‚˜๋„ ์—†์„ ๋•Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ทธ ์™ธ์— ๊ฒฝ์šฐ์—๋Š” board ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๋“ค ์—๋‹ค๊ฐ€ ์™ธ๋ž˜ํ‚ค ๊ฐ’์— ์ผ์น˜ํ•˜๋Š” user ํ…Œ์ด๋ธ”์˜ name ๊ฐ’์„ result ๊ฐ์ฒด์— ํ•ฉ์ณ์„œ callback ํ•จ์ˆ˜๋กœ app.js์— ์ „๋‹ฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.



app.js

1
2
3
4
5
6
7
8
9
app.get('/commuity',function(req,res){
    board.boardLoadFunction(function(result){
        var jsonStr = JSON.stringify(result);
        if(result.length==0)
            res.render('commuity',{data:0})
        else
            res.render('commuity',{data:jsonStr})
    })
});
cs


๊ฒŒ์‹œํŒ์˜ subject, contentํ•„๋“œ์™€ ๋”๋ถˆ์–ด user ํ…Œ์ด๋ธ”์˜ ์ •๋ณด๊นŒ์ง€ ํ•ฉ์ณ์ง„ json ๊ฐ์ฒด๋ฅผ client ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.




board ํ…Œ์ด๋ธ”์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” subject์™€ contentํ•„๋“œ ์™ธ์— ์™ธ๋ž˜ํ‚ค๋กœ user ํ…Œ์ด๋ธ”์˜ name ํ•„๋“œ๋ฅผ ์–ป์–ด์™€์„œ ํ•ฉ์ณ์ง„ ๊ฐ์ฒด๋กœ ์ž‘์„ฑ์ž๊นŒ์ง€ ์ถœ๋ ฅ์„ ํ•ด์ค„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ