2017-03-28 4 views
0

私の問題は、入力ボックスからSQLテーブルにデータを入力することができますが、問題はテーブルのテキストが必要なページに表示されないことですに。何がうまくいくのは、プレイリストページには、テキスト以外のプレイリストの数に応じてhrefリンクが表示されるということです。Node.js SQLデータベース内のテーブルからのSELECT * from

セキュリティ上の理由から、データベースの詳細を除外しました。

playlist.jade

extends layout 

block content 
    h1= title 
    p Welcome to #{title} 

    h1 My Playlists 
    br 
    a(href='/login') Login 
    br 
    a(href='/users/createPlaylist') Create a new Playlists 
    br 
    a(href='/users/playlistCreated') test 
    br 

    for playlist, i in playlists 
    a(href='/users/playlistCreated') | #{playlist.text} 
    br 

users.js

var express = require('express'); 
var mysql = require('mysql'); 
var router = express.Router(); 

var dbConnectionInfo = { 
    host : '', 
    user : '', 
    password : '', 
    database : 'audio_collections' 
}; 

router.get('/createPlaylist', function(req, res, next) { 
    res.render('new_playlist'); 
}); 

router.get('/playlistCreated', function(req, res, next) { 
    res.render('name_of_created_playlist'); 
}); 

router.get('/songCreated', function(req, res, next) { 
    res.render('song_page'); 
}); 

router.get('/newSong', function(req, res, next) { 
    res.render('new_song'); 
}); 

router.post('/newPlaylist', function(req, res, next) { 

    var dbConnection = mysql.createConnection(dbConnectionInfo); 
    dbConnection.connect(); 

    dbConnection.on('error', function(err) { 
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') { 
     // Let's just ignore this 
     console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring '); 
    } else { 
     // I really should do something better here 
     console.log('Got a DB Error: ', err); 
    } 
    }); 

    var playlist = { 
    text: req.body.thePlaylist 
    }; 


    dbConnection.query('INSERT INTO Playlists (playlist_name) VALUES(?)',  [playlist.text], function(err, results, fields) { 
// error will be an Error if one occurred during the query 
// results will contain the results of the query 
// fields will contain information about the returned results fields (if any) 
    if (err) { 
     throw err; 
    } 

    // notice that results.insertId will give you the value of the AI (auto-increment) field 
    playlist.id = results.insertId; 

    // Going to convert my joke object to a JSON string a print it out to the console 
    console.log(JSON.stringify(playlist)); 

    // Close the connection and make sure you do it BEFORE you redirect 
    dbConnection.end(); 

    res.redirect('/'); 
    }); 

    router.post('/newSongAdded', function(req, res, next) { 

    var dbConnection = mysql.createConnection(dbConnectionInfo); 
    dbConnection.connect(); 

    dbConnection.on('error', function(err) { 
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') { 
     // Let's just ignore this 
     console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring '); 
    } else { 
     // I really should do something better here 
     console.log('Got a DB Error: ', err); 
    } 
    }); 

    var song = { 
    text: req.body.theSong, 
    url: req.body.theSongURL 
    }; 

    dbConnection.query('INSERT INTO Songs (song_name, song_url) VALUES(?,?)',[song.text, song.url], function(err, results,fields) { 
    // error will be an Error if one occurred during the query 
    // results will contain the results of the query 
    // fields will contain information about the returned results fields (if any) 
    if (err) { 
     throw err; 
    } 

    // notice that results.insertId will give you the value of the AI (auto-increment) field 
    song.id = results.insertId; 

    // Going to convert my joke object to a JSON string a print it out to the console 
    console.log(JSON.stringify(song)); 

    // Close the connection and make sure you do it BEFORE you redirect 
    dbConnection.end(); 


    res.redirect('/'); 
    }); 

}); 
}); 

module.exports = router; 

index.js

var express = require('express'); 
var mysql = require('mysql'); 
var router = express.Router(); 

var dbConnectionInfo = { 
    host : '', 
    user : '', 
    password : '', 
    database : 'audio_collections' 
}; 

/* GET home page. */ 
router.get('/login', function(req, res, next) { 
    res.render('login'); 
}); 

router.post('/login', function(req, res, next) { 
    var username = req.body.username; 

    username = username.trim(); 

    if (username.length == 0) { 
    res.redirect('/login'); 
    } 
    else { 
    req.session.username = username; 
    res.redirect('/'); 
    } 
}); 

router.get('/', function(req, res, next) { 
    var dbConnection = mysql.createConnection(dbConnectionInfo); 
    dbConnection.connect(); 

    dbConnection.on('error', function(err) { 
    if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') { 
     // Let's just ignore this 
     console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring '); 
    } else { 
     // I really should do something better here 
     console.log('Got a DB Error: ', err); 
    } 
    }); 

    dbConnection.query('SELECT * FROM Playlists', function(err, results, fields){ 
    if (err) { 
     throw err; 
    } 

    var allPlaylists = new Array(); 

    for (var i = 0; i < results.length; i++) { 
     var playlist = { 
     id: results[i].id, 
     text: results[i].text 
     }; 

     console.log(JSON.stringify(playlist)); 

     allPlaylists.push(playlist); 
    } 

    dbConnection.end(); 

    res.render('playlists', {playlists: allPlaylists}); 
    }); 

    router.get('/users/playlistCreated', function(req, res, next) { 
    var dbConnection = mysql.createConnection(dbConnectionInfo); 
    dbConnection.connect(); 

    dbConnection.on('error', function(err) { 
     if (err.code == 'PROTOCOL_SEQUENCE_TIMEOUT') { 
     // Let's just ignore this 
     console.log('Got a DB PROTOCOL_SEQUENCE_TIMEOUT Error ... ignoring '); 
     } else { 
     // I really should do something better here 
     console.log('Got a DB Error: ', err); 
     } 
    }); 

    dbConnection.query('SELECT * FROM Songs', function(err, results, fields){ 
     if (err) { 
     throw err; 
     } 

     var allSongs = new Array(); 

     for (var i = 0; i < results.length; i++) { 
     var song = {}; 
     song.id = results[i].id; 
     song.text = results[i].text; 
     song.url = results[i].url; 

     console.log(JSON.stringify(song)); 

     allSongs.push(song); 
     } 

     dbConnection.end(); 

     res.render('name_of_created_playlist', {songs: allSongs}); 
    }); 
    }); 
}); 

module.exports = router; 

new_playlist.jade

extends layout 

block content 

    form(method='POST', action='/users/newPlaylist') 
     div 
     label(for='thePlaylist') Name of Playlist 
     div 
      textarea(type='text', name='thePlaylist') 
      br 
      input(type='submit', name='Add new Playlist') 

    a(href='/') Cancel 
ここでは

は、私は助けを本当に感謝して、私は今、一週間のために、この上で立ち往生されているデータベースとテーブルのセットアップ

database and table setup

です。それは私が私の配列

のみ必要とするvar allPlaylist =結果内側のループのために必要なdidntの

答えて

0

を固定ガット。結果は既に配列なので

関連する問題