Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: res.json is not a function

I'm trying to send two json but It doesn't work. It prints TypeError: res.json is not a function but I don't get why It happens. Is there any ideas? Thank you !!

app.post('/danger', function response(req, res) {
    let placeId = req.body.data;
    let option = {
      uri: 'https://maps.googleapis.com/maps/api/directions/json?',
      qs: {
        origin:`place_id:${placeId[0]}`, destination: `place_id:${placeId[1]}`,
        language: 'en', mode: 'walking', alternatives: true, key: APIKey
      }
    };
    rp(option)
      .then(function(res) {
        let dangerRate = dangerTest(JSON.parse(res), riskGrid);
        res.json({ data: [res, dangerRate]});
      })
      .catch(function(err) {
        console.error("Failed to get JSON from Google API", err);
      })
});
like image 617
boombamboo Avatar asked Feb 06 '17 19:02

boombamboo


Video Answer


1 Answers

I got this error message because I had the arguments in the wrong order in the handler method. (amateur's fault)

Wrong order: (res, req)

app.get('/json', (res, req) => {
  res.json({
    "message": "Hello json"
  });
});

Right order: (req, res)

app.get('/json', (req, res) => {
  res.json({
    "message": "Hello json"
  });
});
like image 181
Fotis Kolytoumpas Avatar answered Oct 25 '22 06:10

Fotis Kolytoumpas