如何从另一条路线(POST方法)获取数据?

时间:2019-04-23 05:08:35

标签: node.js

我有多个路由设置。我想从另一条路线中获取特定数据。这些数据来自发布方法。

我的server.js看起来像这样:

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)
})

我的路线/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

还有我的路线/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

我想从users.js中获取order.js中的list_items和list_title,这两个数据来自POST方法,如在users.js中所见

1 个答案:

答案 0 :(得分:0)

如果我对您的理解正确,那么您想使用另一条路线/users上的/orders的请求正文。

您正在将用户数据list_titlelist_item保存在users表中。因此,从/orders路由访问数据所需要做的就是对db进行附加查询,在该数据库中,您将通过id \ email选择用户。

不确定使用哪个数据库ORM,但通常情况下,您的代码可能如下所示:

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');
    }
  });
});