D. Hawkins D. Hawkins - 1 year ago 61
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')));
extended: false
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;

// 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);

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) => {
.then(todos => {
res.render('all', {
todos: todos

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

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

router.post('/', (req, res) => {
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];
} else {
// respond with an error
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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download