在 Node API 中调用 Postgres 数据库函数

Calling a Postgres db function in Node API

我正在尝试在我的 queries.js

中调用此 Postgres 函数 core.get_age(bigint)

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

我在 index.js

中的任何路线

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

当我在 POSTMAN 中调用 http://localhost:8080/api/age/10 时,出现 404 错误。

怎么了?这是我尝试调用 Postgres function() 的第一个实例。只是试图用 select * from this.this table 检索 Postgres table 工作但功能我得到这个 404.

啊……

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

而不是我使用的那个。 DB.ANY 获胜。

始终使用 .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();
    })
}