javascript - Node.js SELECT*FROM a table in a SQL database -
my problem can enter data input boxes sql table, problem text table not show on page want to. works playlist page show href links depending on how many playlists have made not text.
i have left out database details security reasons.
playlist.jade
extends layout block content h1= title p welcome #{title} h1 playlists br a(href='/login') login br a(href='/users/createplaylist') create new playlists br a(href='/users/playlistcreated') test br playlist, 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 ignore console.log('got db protocol_sequence_timeout error ... ignoring '); } else { // should better here console.log('got db error: ', err); } }); var playlist = { text: req.body.theplaylist }; dbconnection.query('insert playlists (playlist_name) values(?)', [playlist.text], function(err, results, fields) { // error error if 1 occurred during query // results contain results of query // fields contain information returned results fields (if any) if (err) { throw err; } // notice results.insertid give value of ai (auto-increment) field playlist.id = results.insertid; // going convert joke object json string print out console console.log(json.stringify(playlist)); // close connection , make sure before 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 ignore console.log('got db protocol_sequence_timeout error ... ignoring '); } else { // should better here console.log('got db error: ', err); } }); var song = { text: req.body.thesong, url: req.body.thesongurl }; dbconnection.query('insert songs (song_name, song_url) values(?,?)',[song.text, song.url], function(err, results,fields) { // error error if 1 occurred during query // results contain results of query // fields contain information returned results fields (if any) if (err) { throw err; } // notice results.insertid give value of ai (auto-increment) field song.id = results.insertid; // going convert joke object json string print out console console.log(json.stringify(song)); // close connection , make sure before 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' }; /* 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 ignore console.log('got db protocol_sequence_timeout error ... ignoring '); } else { // should better here console.log('got db error: ', err); } }); dbconnection.query('select * playlists', function(err, results, fields){ if (err) { throw err; } var allplaylists = new array(); (var = 0; < 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 ignore console.log('got db protocol_sequence_timeout error ... ignoring '); } else { // should better here console.log('got db error: ', err); } }); dbconnection.query('select * songs', function(err, results, fields){ if (err) { throw err; } var allsongs = new array(); (var = 0; < 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
here database , table setups
i appreciate , have been stuck on week now.
got fixed didnt need loops inside array
only needed var allplaylist = results; since results array already
Comments
Post a Comment