[英]My API endpoint is not returning the expected output
我已經使用Express JS編寫了一個后端應用程序,以通過API端點的GET請求從數據庫中檢索信息。 我曾經從API端點獲取的查詢正在數據庫中運行,但是我無法在Web瀏覽器中獲取與使用Express JS的響應相同的詳細信息。 盡力找出問題所在,但我做不到。
我試圖檢查我的查詢是否有問題,但是查詢獲取的是正確的詳細信息,但是我無法在Web瀏覽器中得到任何響應。
這是我認為存在錯誤的代碼:
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查詢:
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;
錯誤日志:
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.
嘗試使用回調功能。
con.query("YOUR SQL QUERY HERE", function (err, result, fields) {
if (err) throw err;
console.log(result);
});
參數替換示例
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);
});
我懷疑使用$1%
會導致您的sql語句出現問題。 請嘗試以下方法。
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.