ramkrushna
ramkrushna

Reputation: 27

TypeError: res.send is not a function error accoured

I got "TypeError: res.send is not a function" this error in the given code

const express = require('express');
const bodyParser=require('body-parser');

const app=express();
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/' , (req , res)=>{

   res.sendFile(__dirname+"/index.html")

})
app.post('/' , (req , res)=>{
    // console.log(req.body);
    var num1=req.body.num1;
    var num2=req.body.num2;
    var res=num1+num2;
   res.send('Ans is: '+res);

});
app.listen(3000,function(){
    console.log('running');
    
});

plzz help me to fix this :(

and the error looks like this:-

TypeError: res.send is not a function
    at C:\Users\ADMIN\Desktop\calculator\calculator.js:16:8
    at Layer.handle [as handle_request] (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\ADMIN\Desktop\calculator\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\ADMIN\Desktop\calculator\node_modules\body-parser\lib\read.js:130:5
    at invokeCallback (C:\Users\ADMIN\Desktop\calculator\node_modules\raw-body\index.js:224:16)  

Upvotes: 0

Views: 695

Answers (2)

Subhadip Das Gupta
Subhadip Das Gupta

Reputation: 21

you just override res again in the above line.. var res=num1+num2;

change this to anything else like

var result =num1+num2; res.send('Ans is: '+result);

Upvotes: 0

Joe
Joe

Reputation: 42666

You overwrite res with your own local variable:

    var res=num1+num2;

This "shadows" the res in your function declaration.

Upvotes: 1

Related Questions