Skip to content

Instantly share code, notes, and snippets.

@masmithrpg

masmithrpg/index.js

Created Oct 5, 2018
Embed
What would you like to do?
HY010 error
var db = require('idb-connector')
var body_parser = require('body-parser')
var express = require('express')
var app = express()
var dbconn = new db.dbconn()
dbconn.conn("*LOCAL")
var stmt = new db.dbstmt(dbconn)
var schema = 'NODELIB'
app.set('views', __dirname + '/views')
app.set('view engine', 'pug')
app.use(body_parser.urlencoded({ extended: true }));
app.get('/', function(req, res) {
res.render('index', { title: 'Hey', message: 'Hey Jade!'})
})
app.get('/customers', function(req, res)
{
let stmt = new db.dbstmt(dbconn)
stmt.exec(`Select LSTNAM, CUSNUM from ${schema}.CUSTOMER`, function(results, err)
{
res.render('customers/index', { title: 'Customers', results: results})
stmt.close()
})
})
app.get('/customers/new', function(req, res) {
console.log('i got to /customers/new:' )
res.render('customers/new', {result: {}, form_action: '/customers/create'})
})
app.post('/customers/create', function(req, res) {
let stmt = new db.dbstmt(dbconn)
console.log('now im going to insert data:' )
var sql =
`INSERT INTO ${schema}.CUSTOMER (CUSNUM,LSTNAM,INIT,STREET) VALUES (${req.body.CUSNUM}, '${req.body.LSTNAM}', '${req.body.INIT}', '${req.body.STREET}')`
stmt.exec(sql, function(result, err){
res.redirect('/customers')
stmt.close()
})
})
app.get('/customers/:id', function(req, res) {
let stmt = new db.dbstmt(dbconn)
var sql = `SELECT * FROM ${schema}.CUSTOMER WHERE CUSNUM=${req.params.id}`
stmt.exec(sql, function(result, err) {
res.render('customers/show', { title: 'Customer', result: result[0]})
stmt.close()
})
})
app.get('/customers/:id/edit', function(req,res){
var sql = `SELECT * FROM ${schema}.CUSTOMER WHERE CUSNUM=${req.params.id}`
stmt.exec(sql, function(result, err) {
res.render('customers/edit',
{ title: 'Customer',
result: result[0],
form_action: `/customers/${req.params.id}/update`
}
)
})
})
app.post('/customers/:id/update', function(req, res) {
console.log('I made it to the update function')
var sql =
`UPDATE ${schema}.CUSTOMER SET CUSNUM=${req.body.CUSNUM},LSTNAM='${req.body.LSTNAM}',INIT='${req.body.INIT}',STREET='${req.body.STREET}' WHERE CUSNUM=${req.body.CUSNUM}`
stmt.exec(sql, function(result, err){
console.log(err)
res.redirect('/customers')
})
})
app.get('/customers/:id/delete', function(req, res) {
var sql = `DELETE FROM ${schema}.CUSTOMER WHERE CUSNUM=${req.params.id}`
stmt.exec(sql, function(results, err){
res.redirect('/customers')
})
})
var port = process.env.PORT || 8018
app.listen(port, function()
{
console.log('Running on port %d', port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.