如何将多个GET参数传递给Express?

时间:2018-08-28 09:25:26

标签: node.js express get routes

我对Node.js有点陌生,并在Node.js中编写了一些REST API。对于我的API之一,我需要传递2个参数以从数据库中获取数据。我已经使用单个参数实现了该参数,但不超过2个。有人可以帮助我吗?

这是我的代码

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))
        }
    });
});

我一直在控制台中收到此错误

> /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)

3 个答案:

答案 0 :(得分:1)

以这种方式尝试 在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))
        }
    });
});

其他方式

'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))
            }
        });
    });

另一种方法

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))
            }
        });
    });

答案 1 :(得分:0)

代码中的路由定义模式很好(请检查this thread),问题有所不同:您应该使用单个数组传递所有查询值(docs),不是每个参数的单独数组。例如:

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

请注意,这里的错误消息实际上是有帮助的:Object 1 has no method apply表示MySQL软件包尝试将您的参数视为函数(回调)。

答案 2 :(得分: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))
        }
    });
});