I have multiple routes setup. I want to get specific data from another route. That data is coming from a post method.

My server.js look like this:

var mysql = require('mysql')
var morgan = require('morgan')
var cors = require('cors')
var bodyParser = require('body-parser')
var http = require('http')
var dateFormat = require('dateformat')
const port = process.env.PORT || 3000;



//middleware
var app = express()
app.use(cors())
app.use(morgan('dev'))
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: true
}))

var now = new Date()




//routes
var user_details = require('./routes/users')
var user_orders = require('./routes/order')


//Use routes
app.use('/', user_details)
app.use('/', user_orders)




//Launch Server
app.listen(port, () => {
    console.log('Server start at port: ' + port)
})

My routes/users.js :

var router = express.Router()
var db = require('../dbConfig')
var randomstring = require("randomstring");
var moment = require('moment')


router.post('/list', (req, res) => {
    var appendRandomString = randomstring.generate({
        length: 10,
        capitalization: 'uppercase',
        readable: true
    })
    var id = 'PEPPR_' + appendRandomString
    var email = req.body.email
    var listItems = req.body.listItems
    var listTitle = req.body.listTitle
    var date = moment().format("Do MMMM YYYY");
    var time = moment().format("LT");



    const INSERT_USER_LISTS = `INSERT INTO user_lists (id,date,time,user_email,list_title,list_items) VALUES('${id}','${date}','${time}','${email}','${listTitle}','${listItems}')`
    db.query(INSERT_USER_LISTS, (err, success) => {
        if (err) {
            return res.send(err)
        } else {
            console.log('list added')
            res.send('list added')
        }
    })

})


module.exports = router

And my routes/order.js

var router = express.Router()
var db = require('../dbConfig')
var randomstring = require("randomstring");
var moment = require('moment')
var user_details = require('./users')

router.post('/sendOrder', (req, res) => {
    var email = req.body.email
    var status = 'Order Confirmed'
    var date = moment().format("Do MMMM YYYY");
    var time = moment().format("LT");
    var appendRandomString = randomstring.generate({
        length: 10,
        capitalization: 'uppercase',
        readable: true
    })
    var id = 'PEPPR_ORDER_' + appendRandomString
    var list_items = ''
    var list_title = ''

    var data = {
        id: id,
        email: email,
        list_items: list_items,
        list_title: list_title,
        date: date,
        time: time,
        status: status
    }

    const CREATE_ORDER = `INSERT INTO user_orders SET ?`
    db.query(CREATE_ORDER, data, (err, success) => {
        if (err) {
            return res.send(err)
        } else {
            res.send('oc')

        }
    })
})

module.exports = router

I want the list_items and list_title in my order.js from users.js , this two data is coming from a POST method as you can see in users.js

1 Answers

0
Hilarion Galushka On

If I understood you correctly you want to use request body which comes to /users in another route /orders.

You are saving user data list_title and list_item in users table. So all you need to access the data from /orders route is make an additional query to db where you will select users by id\email.

Not sure which db ORM you use but in general cases your code may look like this:

router.post('/sendOrder', async (req, res) => {
  const { email } = req.body;
  // declare other fields

  const user = await db.query(
    `SELECT * FROM users WHERE email LIKE '%${email}%'`,
    (err, success) => {
      if (err) {
        return res.send(err);
      } else {
        res.send('ok');
      }
    }
  );

  // declare `data` object with user.list_title, user.list_item

  const CREATE_ORDER = `INSERT INTO user_orders SET ?`;
  db.query(CREATE_ORDER, data, (err, success) => {
    if (err) {
      return res.send(err);
    } else {
      res.send('ok');
    }
  });
});