How can I solve this problem, res.json is not a function

Viewed 672

My problem is the following, I send data with guzzle, and got error

TypeError: res.json is not a function.

$client = new \GuzzleHttp\Client();
$response = $client->request('get', 'http://localhost/data', ['total' =>  ['total' => 15]);

I tried the following 2 ways, but they don't seem to work.

exports.data= function(res,req){
    return res.json({ result: 'success' });
};
exports.data= function(res,req){
   res.json({ result: 'success' });
};

Node is working on port 3000.

Error code: TypeError: res.json is not a function Or php page Type: GuzzleHttp\Exception\ServerException

Message: Server error: GET http://localhost:3000/data resulted in a 500 Internal Server Error response: TypeError: res.j (truncated...)

1 Answers

The first parameter in callback method is req ,and then its res

so your method should be

exports.data= function(req,res){
    return res.json({ result: 'success' });
};
Or
exports.data= function(req,res){
    res.json({ result: 'success' });
};

You have interchanged their positions.That's why you are getting the error.

Related