Pass the dropdown selected value to NodeJS for querying database

i have created one dropdown that passes data to NodeJS through axios post i can see that in console log of nodejs Post but cant able to use the value outside the post function

i want to use the value to querying the database

my nodejs code:

  app.post('/getmodel', (req, res) => {
      var model = req.body.model;
    
    console.log(model);
    //It shows model value here but can't able to use outside       
    
  });


app.get('/model', (req,res)=>{
    let model = req.body.model;
 let sql ="select * from new_schema.model_list,new_schema.images where model_name=  " + mysql.escape(model)
 db.query(sql,model, (err,results) =>{
        if(err){
            throw err
        }
         console.log(results)
        res.send(results);
    })
})

my react code works fine as i can able to see the selected value in nodejs console below

Pass the dropdown selected value to NodeJS for querying database

these are the selected value from my dropdown that shows in my nodejs console. but in cant use it by req.body like that please help me

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can try like this

app.post('/getmodel', (req, res) => {
    var model = req.body.model;

    console.log(model);
    //It shows model value here and you can able to use in the query

    let sql = "select * from new_schema.model_list,new_schema.images where model_name=  " + mysql.escape(model)
    db.query(sql, model, (err, results) => {
        if (err) {
            throw err
        }
        console.log(results)
        res.send(results);
    })

});


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x