将mysql查询返回给nodejs Flash消息

时间:2018-11-05 12:12:52

标签: javascript mysql node.js express

我想按办公室号码查询跟踪号。然后将跟踪编号传递到帖子中的req.flash。我能够将数据记录到控制台,将其返回到主节点程序时遇到了问题,因此我可以在req.flash消息中使用该数据。如何将查询结果发送到主要的javascript程序,以便可以在req.flash中刷新查询结果?

我尝试了几个不同的电话。

这是我从控制台输出的信息

[nodemon] starting `node src/server.js`
body-parser deprecated undefined extended: provide extended option src\server.js:20:14
App running at http://localhost:3000
Sanitized: { officeNumber: '1212' }
the output is 0
result from db is :  12121212121212
result from db is :  124563356743435
result from db is :  354466436646464
result from db is :  363548367868575

routes.js

const express = require('express')
const router = express.Router()
const { check, validationResult } = require('express-validator/check')
const { matchedData } = require('express-validator/filter')
var mysql   = require('mysql'),
    config  = require("./config");

    var con = mysql.createConnection(config.db);



router.post('/trackingnumbers', [
  check('officeNumber')
    .isLength({ min:4, max:4  })
    .withMessage('That office Number doesn‘t look right')
    .trim()
], (req, res) => {
  const errors = validationResult(req)
  if (!errors.isEmpty()) {
    return res.render('trackingnumbers', {
      data: req.body,
      errors: errors.mapped()
    })
  }

  const data = matchedData(req)
  console.log('Sanitized:', data)
var mysql_query3 = require('./mysql_query.js');
//////////////////////////////////////////////////////////
output = "0";



function getTrackingNumbers(officeN, callback)
{
    con.query('SELECT * from trackingnumbers where office_Number = ' + officeN, function(err, result)
    {
        if (err) 
            callback(err,null);
        else
            //console.log(result);   

            for (var i = 0; i < result.length; i++) {
              //  console.log(result[i]);
                //callback(null,result[i]);
                callback(null,result[i].tracking_Number);
            }

    });

}


//call Fn for db query with callback
 getTrackingNumbers(req.body["officeNumber"], function(err,data1){
        if (err) {
            // error handling code goes here
            console.log("ERROR : ",err);            
        } else {            
            // code to execute on data retrieval
            console.log("result from db is : ",data1);
            return data1;
        }    

});
/////////////////////////////////////////////////////////////////////////////
console.log("the output is " +  output);
  req.flash('success', 'Thanks your super cool has been inputted' + output)
  //can do 2 flashes here if need to

  res.redirect('trackingnumbers')
})





module.exports = router

1 个答案:

答案 0 :(得分:0)

你能试试吗

getTrackingNumbers(req.body["officeNumber"], function(err,data1){
    if (err) {
        // error handling code goes here
        console.log("ERROR : ",err);            
    } else {            
        // code to execute on data retrieval
        console.log("result from db is : ",data1);
        req.flash('success', 'Data' + data1);
        return data1;
    }    
});

由于Node是异步的,因此我们将仅在回调中获得数据。


如果要在重定向后显示消息闪烁,可以将数据作为查询参数传递

res.redirect('trackingnumbers/?message=The office number is '+data )

然后在route方法主体内,我们可以检查查询参数'message'并刷新消息。

if (typeof req.query.message !== 'undefined' && typeof req.query.message.trim() != "") {
    req.flash('success', req.query.message)
}
const data = matchedData(req)
console.log('Sanitized:', data)