从另一个控制器调用时,findById方法返回null

时间:2019-12-27 18:00:21

标签: node.js express asynchronous sequelize.js body-parser

我正在尝试使用:p建立一个简单的库存管理API。

  • 具有“数量”属性的“股票”模型
  • “请求”模型也具有“数量”属性(用户请求的数量)

当用户提交请求时,我希望从“库存”数量中减去“请求”数量。

request.controller.js:

const db = require('../models/index');
const request = db.request;
const stock = require('../controller/stock.controller.js');

exports.create = (req, res) => {
    stock.findBy_uuid(req.body.stock_uuid)
        .then(stock => {
            if (stock.qty >= req.body.qty) {
                request.create({
                    stock_uuid: req.body.stock_uuid,
                    requester: req.body.requester,
                    qty: req.body.qty,
                    status: 0
                }).then(() => {
                    //stock.update({ qty: stock.qty - req.body.qty })
                    res.status(200).send("Your request has been submitted");
                });
            } else {
                res.status(400).send({
                    message: "This quantity is no longer available in stock"
                });
            }
        })
        .catch(err => {
            res.status(400).send({
                message: err.message || "Can't find stock with id: " + req.body.stock_uuid
            });
        })
};

stock.controller.js

exports.findBy_uuid = (stock_uuid) => {
    return stock.findByPk(stock_uuid)
};


findByPk方法来自Sequelize ORM,并且工作正常(从带有req.params的app.get调用时),但是当我调用 create 时,响应为“无法读取属性'qty'为空”和console.log(stock.findBy_uuid(req.body.stock_uuid))记录此:

Promise [Object] {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined 
}

1 个答案:

答案 0 :(得分:0)

由于无法复制错误,所以我无法直接回答您的问题,但是我发现了一个实例,其中该链默默地失败了,也许您现在可以提供console.log(stock.findBy_uuid(req.body.stock_uuid))

的日志了

这里拥有的是Promise链,您没有从处理程序返回任何东西,因此以下then()返回了undefined Promise。由于第二个Promise没有错误处理程序或被返回,它将无提示地失败。

const db = require('../models/index');
const request = db.request;
const stock = require('../controller/stock.controller.js');

exports.create = (req, res) => {
    // start for first promise
    stock.findBy_uuid(req.body.stock_uuid)
        // this then block didn't seem to return or throw anything
        // which explains stock.findBy_uuid being an empty Promise
        .then(stock => {
            if (stock.qty >= req.body.qty) {
                // start for second promise
                // added a return
                return request.create({
                    stock_uuid: req.body.stock_uuid,
                    requester: req.body.requester,
                    qty: req.body.qty,
                    status: 0
                }).then(() => {
                    //stock.update({ qty: stock.qty - req.body.qty })
                    res.status(200).send("Your request has been submitted");
                }); 
                // end for second promise which will be ignored by the outer then 
                // since it wasn't returned from the handler
                // no catch so if the second promise fails it will do so silently
            } else {
                res.status(400).send({
                    message: "This quantity is no longer available in stock"
                });
            }
        })  // end of then which return a promise in any case but since handler didn't return any thing 
            // it will be an undefined Promise as you saw in your log 
        .catch(err => {
            res.status(400).send({
                message: err.message || "Can't find stock with id: " + req.body.stock_uuid
            });
        })
};