dlwagner / issue-tracker

issue-tracker built with express, mongoose, mongodb-Atlas
1 stars 0 forks source link

On "New Issue" form POST, category and priority are undefined #18

Closed dlwagner closed 4 years ago

dlwagner commented 4 years ago

On "New Issue" form POST, category and priority are undefined. The form POST action succeeds, however, the document that gets created in mongoDB-atlas does not contain the OBJECTIDs for category and priority because they are coming into the POST controller as undefined. Category and Priority drop-down-select boxes are initially populated via the GET route shown below. The POST route controller takes the form data and POSTs back to mongoDB. For some reason the form code is not picking up the "._id" and "name" from the select drop-downs which is necessary in order for the document to be created correctly in the DB.

Console log output:

from CREATE ISSUE CONTROLLER req.body.title: New bug report req.body.description: New bug report req.body.priority: undefined req.body.category: undefined

The routes: `// Get route - home page router.get('/', secured(), index_controller.index);

// POST route - New Issue form router.post('/newissue', secured(), index_controller.create_issue_post);`

The form code (in Pug): image

The Model: `const mongoose = require('mongoose'); const { Schema } = mongoose;

const IssueSchema = new Schema({ title: { type: String, required: true, max: 100 }, description: { type: String, required: true, max: 200 }, author: { type: String, }, category: { type: Schema.Types.ObjectId, ref: 'Category', }, priority: { type: Schema.Types.ObjectId, ref: 'Priority', }, assignee: { type: Schema.Types.ObjectId, ref: 'User' }, milestone: { type: Schema.Types.ObjectId, ref: 'Milestone' }, project: { type: Schema.Types.ObjectId, ref: 'Project' }, status: { type: Schema.Types.ObjectId, ref: 'Status', }, });`

The form UI:

image

Incorrect document in DB (no category or priority):

{"_id":{"$oid":"5e3eebd792d9362e749d9266"}, "title":"Test - Create New Bug with category", "description":"Test - Create New Bug with category", "author":"joe blow", "__v":{"$numberInt":"0"}}

What a correct document would look like:

{"_id":{"$oid":"5e3e0be1bc0cec08dfd03906"}, "title":"Issue10", "description":"bug in machine", "author":"robsmith" "category":{"$oid":"5e3e0be0bc0cec08dfd038f1"}, "priority":{"$oid":"5e3e0be0bc0cec08dfd038f6"}, "assignee":{"$oid":"5e3e0be1bc0cec08dfd03902"}, "milestone":{"$oid":"5e3e0be0bc0cec08dfd038f7"}, "project":{"$oid":"5e3e0be0bc0cec08dfd038fd"}, "status":{"$oid":"5e3e0bdebc0cec08dfd038ec"}, "__v":{"$numberInt":"0"}}

dlwagner commented 4 years ago

Update: The form is a bootsrap 4 form. It has one row and two columns. The submit button sits in the first column with the title and description. The category and priority sit in the second column and they are not being picked up on form submit. If I move the submit button to the second column, then the opposite occurs. I do not have a solution for this, yet.

dlwagner commented 4 years ago

Temporarily resolved: I eliminated the two columns and put everything in one column. I'm now able to access not only the title and description but also the category and priority and successfully creating documents in the DB. This is not the solution, just a temporary solution. Closed for now.

dlwagner commented 4 years ago

Moved the form(method="POST" action="/newissue") up above the row and this fixed the problem with only one column of data getting submitted. The form has been reverted back to the two column form.

Answered on stack overflow

dlwagner commented 4 years ago

CLOSED