0

I'm a bit new to Node.js and writing some REST APIs in Node.js. For one of my API ,I need to pass 2 parameters to get the data from database.I have achieved it using single parameter but not more than 2. can anybody help me please ?

Here's my code

app.get('/getData/:receiver_id&:sender_id',function(req,res){
    connection.query('SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',[req.params.sender_id],[req.params.receiver_id],function(error,results,fields){
        if(error) throw error;
        else {
        res.json({'result':results})
        console.log(JSON.stringify(results))
        }
    });
});

And I keep getting this error in the console

> /var/www/html/chatApp/node_modules/mysql/lib/protocol/Parser.js:80
        throw err; // Rethrow non-MySQL errors
              ^
TypeError: Object 1 has no method 'apply'
    at Query.Sequence.end (/var/www/html/chatApp/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query.ErrorPacket (/var/www/html/chatApp/node_modules/mysql/lib/protocol/sequences/Query.js:90:8)
    at Protocol._parsePacket (/var/www/html/chatApp/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/var/www/html/chatApp/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/var/www/html/chatApp/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/var/www/html/chatApp/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (/var/www/html/chatApp/node_modules/mysql/lib/Connection.js:502:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:710:14)
    at Socket.EventEmitter.emit (events.js:92:17)
Prashant Gupta
  • 788
  • 8
  • 26
Neha
  • 389
  • 4
  • 8
  • 24
  • You've added exactly the same message; please remove it - and make sure the code IS updated and server IS restarted. – raina77ow Aug 28 '18 at 09:56
  • it still shows the same error – Neha Aug 28 '18 at 10:02
  • Please show how you've rewritten the code then. – raina77ow Aug 28 '18 at 10:04
  • your edited code is incorrect you have to bind params into one array not in two different array. It should be `[req.params.sender_id,req.params.receiver_id] instead of [req.params.sender_id],[req.params.receiver_id]` – Prashant Gupta Aug 28 '18 at 10:16
  • Why on Earth do you still use two arrays to pass the params into MySQL in the _edited_ code? What's the edit then? – raina77ow Aug 28 '18 at 10:16

3 Answers3

1

The route-defining pattern in your code is fine (check this thread for alternatives), the problem is different: you should use a single array to pass all the values for query (docs), not a separate array for each param. For example:

connection.query(
   'SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',
   [req.params.sender_id, req.params.receiver_id],
   function(/*...*/)
);

Note that the error message here is actually helpful: Object 1 has no method apply means that MySQL package attempts to treat your param as a function (a callback).

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • how to solve that error ? because it shows `/var/www/html/chatApp/node_modules/mysql/lib/protocol/Parser.js:80 throw err; // Rethrow non-MySQL errors` – Neha Aug 28 '18 at 09:42
  • Have you changed the code the way described? If so, what exactly's the message? – raina77ow Aug 28 '18 at 09:46
  • yeah I changed the code ,but when I call it from the app it still shows the same error on the consile – Neha Aug 28 '18 at 09:47
  • Ok, what's the error msg? You've shown just the first line of it. – raina77ow Aug 28 '18 at 09:49
  • If I try this code ,the url becomes http://104.131.162.126:3000/getData?receiver_id=1&sender_id=2 and it shows the same log in console .What do i do to solve this error ? – Neha Aug 28 '18 at 11:15
1

Try in this way Form the url in this fromat localhost:portno/getData/:receiver_id/:sender_id Ex: localhost:portno/getdata/12/21

app.get('/getData/:receiver_id/:sender_id',function(req,res){
    connection.query('SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',[req.params.sender_id, req.params.receiver_id],function(error,results,fields){
        if(error) throw error;
        else {
        res.json({'result':results})
        console.log(JSON.stringify(results))
        }
    });
});

Other way

'Ex: localhost:portno/getdata/12&21`
 app.get('/getData/:receiver_id&:sender_id',function(req,res){
        connection.query('SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',[req.params.sender_id, req.params.receiver_id],function(error,results,fields){
            if(error) throw error;
            else {
            res.json({'result':results})
            console.log(JSON.stringify(results))
            }
        });
    });

One More Way to do This

app.get('/getData/:receiver_id&:sender_id',function(req,res){
        connection.query('SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',req.params.sender_id, req.params.receiver_id,function(error,results,fields){
            if(error) throw error;
            else {
            res.json({'result':results})
            console.log(JSON.stringify(results))
            }
        });
    });
Prashant Gupta
  • 788
  • 8
  • 26
0
app.get('/getData/:receiver_id/:sender_id',function(req,res){
    connection.query('SELECT * FROM mytable WHERE sender_id=? AND receiver_id=?',[req.params.sender_id, req.params.receiver_id],function(error,results,fields){
        if(error) throw error;
        else {
            res.json({'result':results})
            console.log(JSON.stringify(results))
        }
    });
});
Vaghani Janak
  • 601
  • 5
  • 14