iT邦幫忙

2

一個node.js+jade+mssql做update的問題

各位大神,小弟剛接觸node.js與jade
這次在實驗mssql的CRUD功能實遇到了一些問題
我參考這位大大http://ithelp.ithome.com.tw/articles/10160463
的程式碼去撰寫,不過我使用的工具是mssql
以下是update.jade的內容

h1=title
    -   userVar=street_artist
    -   if(userVar)                       
        form(method='post',action='/update')
        fieldset
            legend=title
            div.clearfix
                label 真實姓名
                div.input                    input(name='artist[name]',class='xlarge',value=street_artist.name)        
                
                    input(name='artist[fan_club]',class='xlarge',value=street_artist.fan_club)
            div.actions
                input(type='submit',value='儲存',class='btn primary')
                button(type='reset',class='btn') 清除 

以下是index.jade的內容

        table
            thead
                tr
                    th(data-field="artist_id",align="cemter",style="padding:5px").inner 藝人編號
                    th(data-field="name",align="cemter",style="padding:5px").inner 真名
                   
                -each street_artist in items
                    tr
                        td(style="padding:5px")=street_artist.artist_id
                        td(style="padding:5px")=street_artist.name                      
                        td
                          a(href='/update/#{street_artist.artist_id}', class='btn btn-warning') 更新                                         
                          a(href='/delete/#{street_artist.artist_id}', class='btn btn-danger') 刪除

最後這是app.js

app.get('/update/:id', function (req, res) {
    var conn = new sql.Connection(dbConfig);
    var req = new sql.Request(conn);
    conn.connect(function (err) { 
        req.query("SELECT * FROM  street_artist where street_artist.artist_id="+"'"+ req.params.id+"'", function (err, recordset) {
            if (err) {
                console.log(err);
            }
            else {
                
                console.log('search is success.');
                res.render('create', {
                    title : 'Update user',
                    user : rows[0]
                });
            }
            conn.close();
            
        })  
    })

});
app.post('/update', function (req, res) {
    pool.getConnection(function (err, connection) {
        connection.query('update users set ? where id = ?', [{
                id : req.body.user.id,
                name : req.body.user.name,
                description : req.body.user.description
            }, req.body.user.id], function (err, fields) {
            if (err)
                throw err;
        });
        connection.query('SELECT * from users', function (err, rows, fields) {
            if (err)
                throw err;
            res.render('user', {
                title : '使用者列表',
                user : rows
            });
        });
        connection.release();
    });
});

感覺我在觀念上似乎有錯誤,我這樣的作法最後導向到的網頁是.../update/id
但是網頁長這樣,請問我的錯誤在哪裡呢?
http://ithelp.ithome.com.tw/upload/images/20161108/20103256GdRrCyoT9o.png
或是在node.js中MYSQL要如何轉換成MSSQL呢?
感謝各位大大

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

2
jsgao0
iT邦新手 5 級 ‧ 2016-11-09 22:48:15
最佳解答

稍微看了一下程式碼,主要有幾個問題:

  1. 這段程式碼
    var req = new sql.Request(conn);
    換個變數名字吧! 你這樣把request給overwrite了...
  2. 沒回應網頁,應該是你沒做req.render把回應的頁面回覆給client。
  3. 怎麼用MSSQL? 看看官方的說明文件吧... https://azure.microsoft.com/zh-tw/documentation/articles/sql-database-develop-nodejs-simple/

ps. 建議更新不用跳轉頁面,改用popup或其他方式會讓UX提升許多

我要發表回答

立即登入回答