从create和decrement读取数据

xesrikrc  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(218)

正如我在本文中读到的,sequelize increment函数返回错误
我已经完成了下一个代码:

exports.create = function (req, res) {
models.sparepart_request.create({
    codWO: req.body.codWO,
    codSparePart: req.body.codSparePart,
    quantity: req.body.quantity,
    date_request: req.body.date_request,
    codUser: req.body.codUser,
    request_return: req.body.request_return,
    received: req.body.received
}).then(function (item) {
    return models.sparepart.decrement(
        'stock', {
            by: req.body.quantity,
            where: {
                codSparePart: req.body.codSparePart
            }
        }
    );
}).then(function (item) {
    res.status(200);
    res.json(({
        success: true,
        message: 'Query successful',
        data: item //This is null, I need the values from creating
    }));
}).catch(function (error) {
    logger.error(JSON.stringify(error));
    res.json({
        success: false,
        message: 'Query not successful and error has occured cretaing',
        error: error,
        stackError: error.stack
    });
    return res.status(500);
});

问题如下:
我的答案如下:

{
"success": true,
"message": "Query successful",
"data": [
    [
        null,
        1
    ]
]
}

如何获取create项的值?
注意:我试图做一个交易,但总是返回一个错误或停止,并给我一个超时错误。我不知道如何使用示例进行事务处理。

bkkx9g8r

bkkx9g8r1#

您可以使用:

returning: true, // to get updated data back

要从update、destroy和其他函数返回结果,其中只返回受影响的行数,如

return models.sparepart.decrement(
    'stock', {
        by: req.body.quantity,
        where: {
            codSparePart: req.body.codSparePart
        }
        returning: true, // to get updated data back
        plain: true, // for plain object and not raw data
    }
);

相关问题