0

I can't seem to get the data from this post call. The body shows as an empty object {}.

I've tried several versions including these posts with no luck: Express.js req.body undefined

I've also tried different content-types, but that also hasn't worked.

Thoughts? Thanks in advance.

index.js:

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const db = require('./queries.js')

const port = 7000

// create application/json parser
var jsonParser = bodyParser.json()
 
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })     

app.get('/', (req, res) => {
    res.json({
        info: 'Node.js, Express, and Postgres API'
    })
})

app.post('/jothook/', jsonParser, db.jothook)

app.listen(port, () => {
    console.log(`App running on port ${port}.`)
})

queries.js:

const Pool = require('pg').Pool
const { req } = require('express');
const pool = new Pool({
    user: 'testuser',
    host: '167.XX.XX.XX',
    database: 'testdb',
    password: 'testpwd',
    port: 5432,
})

const jothook = (req, res) => {
  var qy = JSON.stringify(req.body);
  var qy = 'INSERT INTO data_test VALUES ' + qy;
    pool.query(qy, (error, results) => {
        if (error) {
            throw error
        }
        res.status(201).send(`Data Inserted`)
    })
};
      
module.exports = {
    jothook
};

post call:

{headers={Content-Type=application/json}, body="'test_data', 'joe', 'smith'", method=POST, mode=cors}
user20180254
  • 13
  • 1
  • 3

0 Answers0