I am very new at HTML, NODE, PUG, etc. I'm trying to build a golf
score application. I have created the first part that allows me to
CRUD the courses. This part is working.
I have 2 tables, courses and scores.
After displaying a list of courses, i select a course and load 6
blank HTML table records to allow entry of golfers and scores each of
the 18 holes.
Now is where i'm confused and need some guidance.
in my .pug file, how do i display an output field that displays a
running total of my score?
On my submit button, i'm not getting to my POST, but i cannot figure
out why
I want to be able to hit save on this screen and either Insert or
Update, depending on whether i have already saved. Should that be
handled within a single POST? It seems like i should, but i don't
know enough about Node to know
I am processing my courses and scores within a single index.js file.
Should i be separating the courses/holes into separate .js files?
Thats enough for now
Thanks for any guidance you can provide
app.get('/scores/keepscore', function(req, res) {
res.render('scores/keepscore', { title: 'ScoreCard' ,form_action: /scores/update
})
})
app.post('/scores/update', function(req, res) {
console.log('i got to the update')
let stmt = new db.dbstmt(dbconn)
console.log(req)
var sql =
INSERT INTO ${schema}.SCORECARD (COURSE,GOLFER, GDATE, HOLE1,HOLE2,HOLE3,HOLE4, HOLE5, HOLE6, HOLE7, HOLE8, HOLE9, HOLE10, HOLE11, HOLE12, HOLE13, HOLE14, HOLE15, HOLE16, HOLE17, HOLE18)
VALUES ('${req.body.COURSE}','${req.body.GOLFER}',CURRENT_DATE, ${req.body.HOLER1}, ${req.body.HOLE2}, ${req.body.HOLE3},${req.body.HOLE4}, ${req.body.HOLE5}, ${req.body.HOLE6},
${req.body.HOLE7}, ${req.body.HOLE8}, ${req.body.HOLE9},${req.body.HOLE10}, ${req.body.HOLE11}, ${req.body.HOLE12},${req.body.HOLE13}, ${req.body.HOLE14}, ${req.body.HOLE15},
${req.body.HOLE16}, ${req.body.HOLE17}, ${req.body.HOLE18} ) with NC
stmt.exec(sql, function(result, err){
console.log(err)
res.redirect('/scores/keepscore')
stmt.close()
})
})
Top comments (7)
Alright so: make sure your form's
action
attribute points back to the path of your "save" route (/scores/update
), since otherwise it posts to the current URL. That should get you as far as executing the route code. Handling insert vs update independently is beneficial if there are complexities to the process but at this scale is essentially a matter of taste, as is splitting routes out into independent files. You can display a running score after a post or reload by passing the score in the second argument tores.render
and interpolating it into your template, or dynamically by creating an auxiliary route and using something like jQuery to invoke it asynchronously on a timer, pushing updates into a placeholder DOM element. The former is easier but it's worth knowing how to do the latter as well. Search up tutorials to find out more, using technology names and what you want to do as keywords works pretty well.Also, my guy, check out the edit functionality and the editor docs, parsing this is rough.
Thanks for the reply. I apologize for the the posting of the code. I had enough of a difficult time, just getting my first post to publish.
I have made some incremental progress. I fixed the issue with the submit button not going to the route. That actually turned out to be formatting in my pug file such that the form was not closing properly.
I have 2 primary issues at the moment. I'll try to post code at the bottom
when i render scores/keepscore, i pass the course name and i have that displaying in my table as a header. However, this is not passing back to my /scores/update route.
This leads me to my second issue, when i pass the table to /scores/update and take a look at my sql statement, i can see that all the table elements are appearing at once. So i know that i need to look through the table, but i'm not sure how to accomplish this.
Thanks for any help you can provide.
When a form posts back, it only sends the values of named
input
s (andselect
s andtextarea
s etc). Everything else, including your table, is just static content. So if you want the course to be available in the update route, you need an input to hold that information. You can use the "hidden" input type to ensure the user doesn't have to see it.I'm not really following what you're trying to say with the second issue. Remember your HTML table doesn't exist as far as the update route is concerned: it only cares about what's in those
inputs
, and those get parted out to fields inreq.body
automatically. Your SQL statement looks correct at a glance, althoughwith NC
is new to me -- wait, is this DB2 syntax?!Yes, this is DB2 systax. NC is No Commit. It is used when the database is not journaled.
I have been chasing down processing the req.body all day without luck.
to explain what i meant with my second issue, take a look at the following. I have entered 1 golfer and his score
I do see a clearer picture when i output req.body to the console, however, i cannot figure out how to process it . Apparently i cannot iterate through it, which means i need to do something with it so that i can iterate.
just a snipped of the req.body
req.body
is a hash, not an array. You can't iterate it, but you shouldn't need to. If you want the scores for the fifth hole, for example, that'sreq.body.PAR5
based on how you've named your input elements. That is an array since you have multiple inputs namedPAR5
, so Mike's score should bereq.body.PAR5[0]
.I was close.
The example i came across,showed req.body[0].PAR5
thanks for the help.
Unfortunately, i don't find much time to work on this, but today i got back to it and found a problem. I don't know if it is an issue i have to deal with because of HTML or because of body.parser.
I have a golf course named 'Highland Park'
i pass the course in as result in my .pug file and it displays properly.
I then have a hidden field in my body for the course name
td: b <input type="hidden" name = "COURSE" value= #{result}
when i process my hash, course shows as 'Highland'
it doesn't appear to be a size issue, but something to do with the space.
If i change the course name to 'HighlandPark', my hash shows 'HighlandPark'
Hope that makes sense.
Any idea how to handle this?