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

Pakz
  • 123
  • 2
  • 15

3 Answers3

0

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

var townid = req.body.townid

Sukhi
  • 13,261
  • 7
  • 36
  • 53
  • and in the query part? '$townid' or '${townid}' – Pakz Apr 24 '20 at 03:04
  • 1. I don't know node. js in deapth but ${townid} would work. See [this](https://stackoverflow.com/a/32695337/1367159) for more details. 2. Debug and check what is the content of 'req.body'. It'll throw some light. – Sukhi Apr 24 '20 at 03:11
0
//node
var townid = req.body['townid']

try this maybe this might work

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