简体   繁体   English

我的API端点未返回预期的输出

[英]My API endpoint is not returning the expected output

I have written a backend app with Express JS to retrieve info from a database through GET requests from an API endpoint. 我已经使用Express JS编写了一个后端应用程序,以通过API端点的GET请求从数据库中检索信息。 The query that I have used to GET from API endpoint is working in the Database , but I could not fetch the same details in a Web Browser as Response using Express JS. 我曾经从API端点获取的查询正在数据库中运行,但是我无法在Web浏览器中获取与使用Express JS的响应相同的详细信息。 Tried my best to find out the issue, but I couldn't. 尽力找出问题所在,但我做不到。

I tried to check whether I had a problem with my query, but the query is fetching the correct details, but I couldn't get any response in the web browser. 我试图检查我的查询是否有问题,但是查询获取的是正确的详细信息,但是我无法在Web浏览器中得到任何响应。

Here is my code where I think I had a bug : 这是我认为存在错误的代码:

      const { rows } = await pool.query(
                'SELECT  branches.ifsc, branches.bank_id, branches.branch, branches.address, branches.city, branches.district, branches.state FROM branches WHERE branches.branch LIKE $1% LIMIT $2 OFFSET $3' ,

        [q, limit, offset]
      );

      res.send(rows[0]);
    });

SQL Query used to Fetch Details: 用于获取详细信息的SQL查询:

    SELECT branches.ifsc, branches.bank_id, branches.branch, branches.address, branches.city, branches.district, branches.state FROM branches WHERE branches.branch LIKE '%RTGS%' ORDER BY branches.ifsc LIMIT 3;

Error Log: 错误日志:

Server is running on 7000
(node:19484) UnhandledPromiseRejectionWarning: error: syntax error at or near "LIMIT"
    at Connection.parseE (/home/kiddo/Desktop/backend/node_modules/pg/lib/connection.js:604:11)
    at Connection.parseMessage (/home/kiddo/Desktop/backend/node_modules/pg/lib/connection.js:401:19)
    at Socket.<anonymous> (/home/kiddo/Desktop/backend/node_modules/pg/lib/connection.js:121:22)
    at Socket.emit (events.js:200:13)
    at addChunk (_stream_readable.js:294:12)
    at readableAddChunk (_stream_readable.js:275:11)
    at Socket.Readable.push (_stream_readable.js:210:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:166:17)
(node:19484) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19484) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Try With Callback function. 尝试使用回调功能。

con.query("YOUR SQL QUERY HERE", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });

parameter substitution Example 参数替换示例

     var sql = "SELECT * FROM table WHERE field1 = ? AND field2 >= ?";
connection.query(sql, [value1, value2], function(err, result, fields) {
    //do your operations HERE
    console.log(result);

});

I suspect using $1% is causing issues in your sql statement. 我怀疑使用$1%会导致您的sql语句出现问题。 Try the following instead. 请尝试以下方法。

const { rows } = await pool.query(
        'SELECT  branches.ifsc, branches.bank_id, branches.branch, branches.address, branches.city, branches.district, branches.state FROM branches WHERE branches.branch LIKE $1 LIMIT $2 OFFSET $3' ,
['%'+q+'%', limit, offset]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM