2017-04-07 36 views
0

我試圖調用此函數的Postgres core.get_age(BIGINT)在我queries.js在調用API節點

function getAge(req, res, next){ 
 
    var customer_id= parseInt(req.params.customer_id); 
 
    db.one('SELECT * FROM core.get_age($1)', customer_id) 
 
    .then(function(data){ 
 
     res.status(200) 
 
     .json({ 
 
      status: 'success', 
 
      data : data, 
 
      message: "Retrieved Age" 
 
     }); 
 
    }) 
 
}

任何我在index.js路線一個Postgres數據庫功能

router.get('/api/age/:customer_id', db.getAge);

當我在POSTMAN中致電http://localhost:8080/api/age/10時,我得到404錯誤。

出了什麼問題?這是我想要調用Postgres函數的第一個實例()。只是想找回Postgres的表select * from this.this表的作品,但與功能我得到這個404

回答

0

始終使用.catch承諾!它會指出問題,就像您的案例中返回的行數無效。

function getAge(req, res, next) { 
    db.func('core.get_age', +req.params.customer_id) 
    .then(data => { 
     res.status(200) 
     .json({ 
      status: 'success', 
      data : data, 
      message: "Retrieved Age" 
     }); 
    }) 
    .catch(error => { 
     console.log(error); 
     next(); 
    }) 
} 
-1

唉唉..

db.any('SELECT * FROM core.get_age($1)', customer_id) 

,而不是一個我用它工作。 DB.ANY獲勝。

+0

原因是你沒有使用承諾的權利。必須始終提供'.catch'處理程序。這樣你可以立即知道問題在哪裏。 –