0

As I've read in this article Sequelize increment function returning error

I have done the next code:

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

The problem is the following:

My JSON answer is the following:

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

How can I take the value of the create item?

NOTE: I've tried to do a transaction but always return a error or stops and gives me a Time Out error. I don't know how to do a transaction with a instance.

1 Answer 1

2

You can use :

returning: true, // to get updated data back 

To get result back from update , destroy and other function where it just returns the no of rows affected , like

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 } ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.