Ad

How Do I Get The Value I Want?

I'm currently working on a react project. I want to send the value(1001) to the server through the axios in view, but the server does not receive the value. I need your help. Thank you.

-----------------view file---------------------------------

handleChange(e) {
    //this.setState({option: e.target.value});
    axios
        .get('/status/bus_info',{
            'data': e.target.value
        })
        .then(function(requset) { 
            console.log(requset);
            requset = requset.config.data;
            console.log(requset);
    })
}

render(){
    return(
        <>
        <div className="selwrap floatL">

            <i className="select_icon"><img src={require('img/icon/icon_handle_g.png')} alt="hi"/></i>selectBox
            <select className="transparentInput" value={this.state.option.value} onChange={this.handleChange}>
            {this.state.mariaComData.map((maria, i) => (
                <option id={maria.COMPANY_ID} key={i} value={maria.COMPANY_ID}>{maria.NAME}</option>

            ))}
            </select>
        </div>
        </>
    )
}

-----------------server file---------------------------------

router.get('/status/bus_info', (req, res) => {
    //console.log(req.data);
   // res.json(req.data)
    let where = '';

    if(!req)
        where = `where company_id = ${data}`;

    db.query(`select * from myTable${where}`, (err, rows) => {
        if (!err) {
            res.send(rows);
        } else {
            res.send(err);
        }
    });
});

-------------view console.log------------

{data: Array(26), status: 200, statusText: "OK", headers: {…}, config: {…}, …}
config:
adapter: ƒ xhrAdapter(config)
baseURL: "http://localhost:4000/"
data: "1001"
headers: {Accept: "application/json, text/plain, */*"}
maxContentLength: -1
method: "get"
timeout: 0
Ad

Answer

Send your data in query parameter of your api

axios.get(`/status/bus_info?data:${e.target.value}`)
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});

and in your api

router.get('/status/bus_info', (req, res) => {
const {data} = req.query;
let where = '';

if(data)
    where = ` where company_id = ${data}`;

db.query(`select * from myTable${where}`, (err, rows) => {
    if (!err) {
        res.send(rows);
    } else {
        res.send(err);
    }
});
});
Ad
source: stackoverflow.com
Ad