내맘대로 살기🎉

[Node.js] OrientDB 데이터 편집 및 삭제. 본문

개발자의 길/Node.js

[Node.js] OrientDB 데이터 편집 및 삭제.

해림😶 2017. 5. 14. 22:09


편집(Edit)


view.jade(뷰 단)에 

- rid = encodeURIComponent(topic['@rid'])

a(href='/topic/'+rid+'/edit') edit

를 추가하여 edit기능을 넣어줄 수 있는 버튼(?)을 넣어준다.


- rid = encodeURIComponent(topic['@rid'])를 넣어주지 않으면 주소에 #이 들어간다. #이 주소창에 있으면 문제가 생길 수 있으므로 특수문자로 변경해주는 코드를 사용해서 #을 다른 문자로 변형시켜준다.



편집을 하기 위해서는 edit.jade에 db에 있는 글을 읽어오는 로직이 추가되어야 한다.


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
app.get('/topic/:id/edit', function(req, res){
    var sql = 'SELECT FROM topic'; // db로 데이터를 받아오려고 추가.
    var id = req.params.id;
    db.query(sql).then(function(topics){
    //fs.readdir('data',function(err,files){ // 이것은 파일으로 데이터를 받아오는 것
        var sql = 'SELECT FROM topic WHERE @rid=:rid';
        db.query(sql,{params:{rid:id}}).then(function(topic){
            res.render('edit', {topics:topics, topic:topic[0]});
        });
    });
});
app.post('/topic/:id/edit', function(req, res){
    var sql = 'UPDATE topic SET title=:t, description=:d, author=:a WHERE @rid=:rid'; // db로 수정된 데이터를 보내려고 추가.
    var id = req.params.id;
    var title = req.body.title;
    var desc = req.body.description;
    var author = req.body.author;
    db.query(sql,{
        params:{
            t:title,
            d:desc,
            a:author,
            rid:id
        }
    }).then(function(topics){
        res.redirect('/topic/'+encodeURIComponent(id));
    });
});
이렇게 post방식으로 코드를 넣어준다.

그리고 edit.jade에 수정할 데이터를 넣어 놓는 것이 중요하다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
article
            - rid = encodeURIComponent(topic['@rid'])
            form(action='/topic/'+rid+'/add' method='post')
                p
                    input(type='text' name='title' placeholder='title' value=topic.title)
                p
                    //- input과 textarea의 값을 읽어올때 value와 =으로 넣은 차이를 알아야해.
                    textarea(name='description' placeholder='description')
                        =topic.description
                p
                    input(type='text' name='author' placeholder='author' value=topic.author)
                p
                    input(type='submit')
<textarea>

이런 식으로 input태그는 value로, textarea태그는 "="을 이용하여 값을 넣어준다.



삭제(Delete)


view.jade에 delete링크를 추가해주어야 한다.


delete.jade를 만들고, delete기능을 만듬.

삭제를 눌렀을 때, 한 번 더 묻는 페이지에서 YES를 링크로 만들지 않는 것을 주의.

- 페이지로 접근하는 것은 get방식을 사용

- 명령(글을 써라, 지워라)을 컴퓨터에게 내리는 것은 반드시 post방식을 사용.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
app.get('/topic/:id/delete', function(req, res){
    var sql = 'SELECT FROM topic'; // db로 데이터를 받아오려고 추가.
    var id = req.params.id;
    db.query(sql).then(function(topics){
    //fs.readdir('data',function(err,files){ // 이것은 파일으로 데이터를 받아오는 것
        var sql = 'SELECT FROM topic WHERE @rid=:rid';
        db.query(sql,{params:{rid:id}}).then(function(topic){
            res.render('delete', {topics:topics, topic:topic[0]});
        });
    });
});
app.post('/topic/:id/delete', function(req, res){
    var sql = 'DELETE FROM topic WHERE @rid=:rid'; // db로 수정된 데이터를 보내려고 추가.
    var id = req.params.id;
    db.query(sql,{
        params:{
            rid:id
        }
    }).then(function(topics){
        res.redirect('/topic/'); // +encodeURIComponent를 하지 않는 이유는, 삭제하면 없어지니까 그냥 topic페이지로 가는 것.
    });
});
<textarea>

이것을 보면 title이나, description같은 것이 없다. 지우는 것이기 때문에 없어도 됨.



Comments