D. Hawkins D. Hawkins - 2 months ago 9
Javascript Question

POST form data logs out to console, but doesn't get sent to the server

I'm building out a simple TO-DO app in Express with Knex as my ORM. Everything is working just fine except for the fact that my form data won't POST to the server from my form. As a debugging measure, I made sure to log the req.body to the console. So the information that I type into my form shows up in the console, so that issue at this point is figuring out why it's not posting to the server. I suspect that my issue is coming solely from the knex query, but I just don't know how or why. Here's what I have so far...

The first file is my app.js
The second contains the POST route in question. Thank you for any help!



var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var users = require('./routes/users');
var todo = require('./routes/todo');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/users', users);
app.use('/todo', todo);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;







var express = require('express');
var router = express.Router();

const knex = require('../db/knex');

/* GET http://localhost:3000/todo page. */
router.get('/', (req, res) => {
knex('todo')
.select()
.then(todos => {
res.render('all', {
todos: todos
});
})
});

router.get('/new', (req, res) => {
res.render('new');
});

function validTodo(todo) {
return typeof todo.title == 'string' && todo.title.trim() != '' && typeof todo.priority == 'undefined' && !isNaN(Number(todo.priority));
}

router.post('/', (req, res) => {
console.log(req.body);
if(validTodo(req.body)) {
const todo = {
title: req.body.title,
description: req.body.description,
priority: req.body.priority,
date: new Date()
};
// insert todo into database
knex('todo').insert(todo, 'id')
.then(ids => {
const id = ids[0];
res.redirect(`/todo/${id}`);
});
} else {
// respond with an error
res.status(500);
res.render('error', {
message: 'Invalid todo'
});
}
});

module.exports = router;




Answer Source

So I figured out what my issue was. For whatever reason, changing the order of my POST route did the trick. I simply placed my POST route over my GET /new route. I'm really not sure how the logic on this works, but I'm glad I got it fixed.