I am trying to do a post request that will do the following.
- Update the database
- Commit the update
- Return the new record in the JSON reponse
Here is the code I have, it works to update the dataase, I threw in some console.logs() to see the flow. the current code outputs "World Hello" instead of "Hello World". How could I make the output be "Hello World" ?
app.post('/checkin', async (req, res) => {
db('assets').select('status').where('id', '=', req.body.id)
.then(data => {
currentStatus = data[0].status;
if (currentStatus.slice(0, 10) === 'In Use By ' || currentStatus === 'Quarantine') {
db('assets')
.where('id', '=', req.body.id)
.update({ status: 'Available', comments: '' }).then(db.commit)
.then(console.log('Hello'))
}
})
.then(console.log('World'))
.then(db('assets').select('*').where('id', '=', req.body.id)
.then(data => res.status(200).json(data[0])))
.catch(error => res.status(400).json(error))
});