43

I am trying to send an integer via response.send() but I keep getting this error

express deprecated res.send(status): Use res.sendStatus(status) instead

I am not sending a Status, my code is

app.get('/runSyncTest' , function(request, response){  

var nodes = request.query.nodes;
var edges = request.query.edges;
if (edges == "" ){
    edges = []
}

userStory.userStory(nodes,edges);
connection.query('SELECT MAX(id) as id FROM report ', function(err,results, fields) {
                idTest = results[0].id
                response.send (idTest)
});

});
ggorlen
  • 44,755
  • 7
  • 76
  • 106
D-W-A
  • 551
  • 2
  • 7
  • 12
  • 3
    just to avoid warning, how about returning JSON like `response.send ({max:idTest})` – Haris Hasan May 30 '15 at 18:57
  • 3
    Regarding `res.send()`, [the fine manual](http://expressjs.com/4x/api.html#res.send) states: _"The body parameter can be a Buffer object, a String, an object, or an Array."_. So integers aren't directly supported and need to be converted to one of those types first. – robertklep May 30 '15 at 19:03
  • the JSON trick worked thank you @HarisHasan – D-W-A May 30 '15 at 19:09

9 Answers9

92

You could try this:

res.status(200).send((results[0].id).toString());

Guys are right - it doesn't allow numbers. Prooflink: http://expressjs.com/4x/api.html#res.send

Arif Dewi
  • 2,222
  • 26
  • 14
25

This is because you are sending numeric value in the res.send.

You could send a json object or convert it to string.

Jerome Miranda
  • 321
  • 4
  • 8
7

(as mentioned in the comments already)

The manual states:

The body parameter can be a Buffer object, a String, an object, or an Array.

So integers aren't directly supported and need to be converted to one of those types first. For instance:

response.send(String(idTest));
robertklep
  • 198,204
  • 35
  • 394
  • 381
5

As long as you're not sending String or Object/Array data you get an error. Solution convert your data to string:

app.get('/runSyncTest', function(req, res) {
    var number = 5000;
    res.send((number).toString()); //Number is converted with toString()
});
RegarBoy
  • 3,228
  • 1
  • 23
  • 44
  • This answer works without any error or warning. Clearly, this is a bug in the error handling of Express. it has a type error but showed the wrong error (deprecated...) – Milad Abooali Jun 12 '23 at 06:40
  • @MiladAbooali if you are using JS, there're no type checks by default in JS, thus TS was introduced. – RegarBoy Jun 12 '23 at 09:08
  • 2
    ,Thanks for reminding that. but I'm not talking about type checks. I meant from a bug is the wrong error message. This is about the Express send method, not JS basics. We expect to see anything like empty input or wrong input but not a deprecated notice! This msg is not related to the error at all – Milad Abooali Jun 13 '23 at 01:09
5

Its Deprecated, Express 5 No Longer Supports The Signature Like -

res.json(200, {
   result: result
});

Instead, use a below method, Means you only need to change the format of sending responses.

res.status(statusCode).json(result);

Example -

res.status(200).json({'success' : true, 'result': result})
Dere Sagar
  • 1,719
  • 1
  • 10
  • 7
3

Use like this,

res.status(404).send('Page Not found');
ddmps
  • 4,350
  • 1
  • 19
  • 34
Anshul Bisht
  • 1,644
  • 20
  • 21
  • 3
    this doesn't solve the issue at all; you are literally just sending an error in response – dlq Dec 31 '20 at 05:21
1

I am getting a deprecated warning in node-express typescript project due to res.send(403);

Solution 1 -

res.sendStatus(403);

Solution 2 -

res.status(403).json(
        {
            'success': false,
            'result': 'forbidden'
        }
    )
Pinaki
  • 792
  • 8
  • 17
0

if you have in your code:

app.use(bodyParser.json());

then you should always return a 'JSON' with the response, for example:

the wrong answer for get request is:

router.get("/", async (req, res) => {
    const result = 3;
    res.send(result);
});

the right answer should be:

router.get("/", async (req, res) => {
    const result = 3;
    res.send({ result });
});
-2

Simply converting it to a string works great.

response.send(idTest.toString()