Manuel Torres
Manuel Torres

Reputation: 3

Node Express, function with parameters inside res.send

Im trying to return a value depending a parameter received. But im getting:

TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object. Received type function ([Function])

app.get('/api/:producto', (req, res) => {

    const strProd = req.params.producto;

    res.send( (strProd) => {
       if (strProd==1) {
           return "1"
       } else {
           return "2"
       }
    })
})

Thanks in advance!

Upvotes: 0

Views: 823

Answers (1)

Bergur
Bergur

Reputation: 4067

As the error message says. The arguement of res.send function must be a string, buffer, array, etc. You're passing a function into the send function.

So simply put the if checks first and then give the appropriate response.

app.get('/api/:producto', (req, res) => {

    const strProd = req.params.producto;

    if (strProd === "1") {
       return res.send("1")
    } else {
       return res.send("2")
    }          
})

Upvotes: 2

Related Questions