'No response when attempting to update Postgres jsonb column
I have a postgres db with a jsonb column that I update via a React frontend and Node backend using Express and Knex to build queries. The way I am going about it works in other sections of my app but for some reason this route isn't working properly. Logging the payload from the front end to the backend shows the data getting passed through however I get no response from the update request.
Frontend request:
const submitImages = async (e) => {
e.preventDefault()
//updates images with inputFields data
console.log('inputFields: ', inputFields)
//try fetch instead
try {
const response = await fetch(`${url}/characters/${props.characterId}`, {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': await context.getAuthToken()
},
body: JSON.stringify(inputFields)
})
console.log('update response', response.data)
} catch (e) {
console.error('Error updating images: ', e)
}
}
Backend Router:
charactersRouter
.route('/characters/:id')
.post(bodyParser, (req, res, next) => {
const idToken = req.headers.authorization
const { id } = req.params
Verification.run(idToken).then((uid) => {
if(uid) {
try {
CharactersService.updateImages(req.app.get('db'), req.body, id)
.then(numRowsAffected => {
console.log('res: ', numRowsAffected)
})
} catch (e) {
console.log('error updating images: ', e)
}
}
})
})
The above function produces the following error: error updating images: TypeError: Cannot read property 'then' of undefined
Update service:
updateImages(knex, update, id) {
console.log('update: ', update)
console.log('id: ', id)
console.log('db: ', knex.client)
update.map((image) => {
try {
console.log(`update characters
SET photos = COALESCE(photos, '[]'::jsonb || ' {"url": "${image.url}", "name": "${image.name}"}' ::jsonb )
WHERE
id = '${id}'`)
return knex.raw(`update characters
SET photos = COALESCE(photos, '[]'::jsonb || ' {"url": "${image.url}", "name": "${image.name}"}' ::jsonb )
WHERE
id = '${id}'`)
} catch (e) {
console.log('error updating images: ', e)
}
})
}
When I take the output of the console.log of the query in updateImages() and paste it directly into dBeaver and run it the column updates correctly.
I logged knex.client to see if there was anything different between this request and working ones and the only difference I saw is the very key is missing in this one. In working code elsewhere in the app the last key of knex.client is version: 14.0.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
