How to Increase counts on click in database using javascript increment method?

Viewed 131

I have field called ads_counter in my database, and I need to increase its value each time when this ad is rendered.

I want to implement increment function of javascript in node rest api when be clicked by users I will get 1 in request I have increase no in database by 1. for example If I have ads_counter = 5 and I get req from client side to increase no by 1. so, how can I increase no. of ads_counter in database. I'm not able to understand from where to start I am using Sequelize with postgres database. please suggest me good method to do this opreation.

2 Answers

You have to add below code in your node api You have to get product_id from the client side and this api will increase the ads_counter by 1.

Product.update({ ads_counter: sequelize.literal('ads_counter+ 1') }, { where: { id: req.body.id } });

You can use increment method like this.

Model.increment('ads_counter', { where: { id: req.body.id } });

this will increment ads_counter by 1.

or you can use this if you want to specify the count number

Model.increment({ads_counter: 2}, { where: { id: req.body.id } })
Related