Hi
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
router.post('/transaction', function (req, res, next){
let desc = req.body.test
console.log(req)
console.log(desc)
res.json({description:desc
})
the form
<form action="/routes/api/transaction" method="post">
<input type="text" name = "test" value="test">
<input type="submit" value="Submit">
</form>
Dereje1
December 24, 2018, 4:34am
2
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.
HI
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`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
app.use(bodyParser.json())
let appApi = require('./routes/api');
app.use('/routes/api', appApi);
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/children.html');
});
app.get("/:word",function(req,res,next){
res.sendFile(__dirname + '/views/accountpage.html');
//let resObj = {"echo":req.params.word};
//res.json(resObj);
});
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'
}
res.status(errCode).type('txt')
.send(errMessage)
})
// 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');
mongoose.connect(process.env.MONGO_DB);
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})
newChild.save((err,person)=>{
if(err) return next(console.log(err))
console.log(person)
response.json(person)
});
});
});
router.post('/transaction', function (req, res){
let desc = req.body.test
console.log(req)
console.log(desc)
res.json({description:desc})
})
module.exports = router;
Thanks for your reply.
Just worked it out.
Sorry - I did not app.use the urlencoded body-parser function.
Sorry
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.