1

Hello i have small problem here but i don't know how to solve it I have a data from flutter "townid" what i want is to query using node.js from flutter's post request

//code from flutter app
  Future getB(townId) async{
 var data;
    print(townId);
    final response = await http.post("http://xxx.xxx.xx.xx:3000/selectB",body:{
      "townid": townId.toString()
    });
    data = jsonDecode(response.body);
    return data;
}

//my node js that receive my flutter request 

const express = require('express');
const bodyParser = require("body-parser");
const mysql = require('mysql');
const router = express.Router();
const db = require('./db');
const app = express();
app.use(bodyParser);

router.post('/selectB', function (req, res) {
      var townid = req.query;  // i dont know if this is the correct way to do this

      db.connect(function(err) {
      //Select all customers and return the result object:
      db.query("SELECT `town_id` as `tid` , `brgy_name` as `brg_name` FROM barangays WHERE `town_id` = $townid ", function (err, result, fields) { // i don't know if this is the correct way to do this
        if (err) throw err;
        res.send(result);
      });
    });
})

3 Answers 3

0

From the node side, you'll have to fetch the townid as :

var townid = req.body.townid

Sign up to request clarification or add additional context in comments.

2 Comments

and in the query part? '$townid' or '${townid}'
1. I don't know node. js in deapth but ${townid} would work. See this for more details. 2. Debug and check what is the content of 'req.body'. It'll throw some light.
0
//node
var townid = req.body['townid']

try this maybe this might work

2 Comments

Thank you for answering but its TypeError: Cannot read property 'townid' of undefined
Please add some explanation to your answer such that others can learn from it
0

since the body of post request from your flutter app is body:{"townid": townId.toString()}); you can parse the boy using body-parser and this is what it looks like

 const townid = req.body.townid

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.