从创建和减少中读取读取数据

时间:2018-04-14 10:13:17

标签: mysql sql express sequelize.js

正如我在本文中所读到的那样Sequelize increment function returning error

我已经完成了下一个代码:

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);
});

问题如下:

我的JSON答案如下:

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

如何获取创建项的值?

注意:我已尝试进行交易但总是返回错误或停止并给我一个超时错误。我不知道如何与实例进行交易。

1 个答案:

答案 0 :(得分:1)

您可以使用:

returning: true, // to get updated data back

要从更新,销毁和其他函数返回结果,它只返回受影响的行数,例如

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
    }
);