Skip to content

Instantly share code, notes, and snippets.

@masmithrpg
Created October 3, 2018 14:18
Show Gist options
  • Save masmithrpg/7d4f25949f54b0d641ffa5068f98870a to your computer and use it in GitHub Desktop.
Save masmithrpg/7d4f25949f54b0d641ffa5068f98870a to your computer and use it in GitHub Desktop.
node.js crud HY010 error
const db = require('/QOpenSys/QIBM/ProdData/OPS/Node6/os400/db2i/lib/db2a')
const body_parser = require('body-parser')
const express = require('express')
const app = express()
const dbconn = new db.dbconn()
dbconn.conn("*LOCAL")
const stmt = new db.dbstmt(dbconn)
const 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 = 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