Please help. Form not posting?


I have this code below but when I post it and console dot log the request the body is empty. I am also sending the json back but all the parameters are empty as well

the handler'/transaction', function (req, res, next){
    let desc = req.body.test

the form

<form action="/routes/api/transaction" method="post">
        <input type="text" name = "test" value="test">
        <input type="submit" value="Submit">

You have not posted enough information, for instance, are you using express or just node on it’s own ? What does your router look like ?
In any event I can see that the callback argument to your middleware function (next()) is not being activated in the post method.
Post more of your code for more clarity.


Thank you for your reply.

I am using node JS.

this is my server.js file.

// server.js
// where your node app starts

// init project
const express = require('express');
const app = express();

const bodyParser = require('body-parser');

// we've started you off with Express, 
// but feel free to use whatever libs or frameworks you'd like through `package.json`.


let appApi = require('./routes/api');
app.use('/routes/api', appApi);

app.get('/', function(request, response) {
  response.sendFile(__dirname + '/views/children.html');

  res.sendFile(__dirname + '/views/accountpage.html');
  //let resObj = {"echo":req.params.word};

app.use((req, res, next) => {
  return next({status: 404, message: 'not found'})

app.use((err, req, res, next) => {
  let errCode, errMessage

  if (err.errors) {
    // mongoose validation error
    errCode = 400 // bad request
    const keys = Object.keys(err.errors)
    // report the first validation error
    errMessage = err.errors[keys[0]].message
  } else {
    // generic or custom error
    errCode = err.status || 500
    errMessage = err.message || 'Internal Server Error'

// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);

this is the rest of my routes file that routes the forms action call.

const router = require('express').Router()
const mongoose = require ('mongoose');
const ModelChild = require ('../models/child');

router.get('/accountpage/:word', function(request,response,next) {
  ModelChild.find({childName:request.params.word}, function(error,data){
    if(error) return next(console.log(error))
    if (data[0]) return (response.json(data[0]))

    const newChild = new ModelChild({childName:request.params.word}),person)=>{ 
      if(err) return next(console.log(err))
});'/transaction', function (req, res){
    let desc = req.body.test

module.exports = router; 

Thanks for your reply.

Just worked it out.

Sorry - I did not app.use the urlencoded body-parser function.


Thanks for your time

Thanks @Dereje1 for looking into it. I figured it out - I really needed to look at “more of the code” myself.