簡體   English   中英

無法使用 node.js 中的請求參數 ID 獲取可用數據

[英]Unable to fetch the available data with the request params id in node.js

我在服務器上有以下獲取請求:

payments.get('/:id', (req, res) => {
    Payment.findAll({
        where: {
            requestID: req.params.id
        }
    })
    .then(res => {
        res.status(200).json({status: "Ok", res})
    })
    .catch(err => {
        res.status(404).json({status: false});
        // res.send(err);
    })
})

當我測試從 postman 中給定的請求參數 id 中獲取所有付款詳細信息時,我未能從數據庫中獲取可用數據,並且它始終運行到 catch() 函數,如下圖所示:

使用傳遞的請求 ID 參數獲取請求

當我在 postman 中點擊發送按鈕時,我的 nodejs 控制台將執行以下查詢函數:

Sequelize 中執行的查詢

當我在 XAMPP 服務器中測試此查詢時,它會顯示數據。

SELECT `payment_id`, `stud_uuid` AS `studID`, `request_id` AS `requestID`, `request_date` AS `requestDate`, `amount`, `ins_uuid` AS `insID`, `status` FROM `tbl_payment` AS `tbl_payment` WHERE `tbl_payment`.`request_id` = 'd690ae99-c6bf-4568-ad2b-980bfb4696e8'

數據如下:

來自已執行查詢的響應

我無法顯示可用數據的 nodejs 函數中缺少什么? 為什么我的 get 請求總是轉到 catch() 語句.. 任何幫助表示贊賞。

問題在這里:

payments.get('/:id', (req, res) => {
    Payment.findAll({
        where: {
            requestID: req.params.id
        }
    })
    .then(res => {
// −−−−−−−^^^
        res.status(200).json({status: "Ok", res})
    })
    .catch(err => {
        res.status(404).json({status: false});
        // res.send(err);
    })
})

您正在使用承諾的結果隱藏您在get回調中收到的res 重命名:

payments.get('/:id', (req, res) => {
    Payment.findAll({
        where: {
            requestID: req.params.id
        }
    })
    .then(result => {
// −−−−−−−^^^^^^
        res.status(200).json({status: "Ok", res: result})
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^
    })
    .catch(err => {
        res.status(404).json({status: false});
        // res.send(err);
    })
})

問題是在這里所使用資源的承諾Payment.findAll的分辨率的對象,而不是使用一些其他的變量,使得資源沒有得到重寫。

payments.get('/:id', (req, res) => {
    Payment.findAll({
        where: {
            requestID: req.params.id
        }
    })
    .then(**done** => {
        res.status(200).json({status: "Ok", **done**})
    })
    .catch(err => {
        res.status(404).json({status: false});
        // res.send(err);
    })
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM