How to implement updateById for an array of items #7351
-
somebody can help me, if I can use forEach, or it suits me other method ??? @put('/rigs-schedule-pus')
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@emazzu , you can traverse the array parameter and I recommend you use updateById method instead of replaceById. In the following example, I receive an array of TaxReturn models and iterate thru each of them. Notice also the OPENAPI spec I placed in the requestBody(). I didn't placed a try{} catch{} block on the updateById in my case, but be aware that it can throw the EntityNotFound error whenever the id of the payload item cannot be found in the database. @put('/replace/taxforms/')
@response(204, {
description: 'TaxReturn PUT success',
})
async updateArrayPayLoad(
@requestBody(
{
content: {
'application/json': {
schema: {
type: 'array',
items: getModelSchemaRef(TaxReturn, {
title: 'NewTaxReturnItem',
}),
}
},
},
}
) taxReturn: TaxReturn[],
): Promise<void> {
for (const rec of taxReturn) {
await this.taxReturnRepository.updateById(rec.id, rec);
}
} |
Beta Was this translation helpful? Give feedback.
-
@marioestradarosa
It works very good !!!! If entity doesnt exist, show message correctly
If entity has error, show message correctly
|
Beta Was this translation helpful? Give feedback.
@emazzu , you can traverse the array parameter and I recommend you use updateById method instead of replaceById. In the following example, I receive an array of TaxReturn models and iterate thru each of them. Notice also the OPENAPI spec I placed in the requestBody().
I didn't placed a try{} catch{} block on the updateById in my case, but be aware that it can throw the EntityNotFound error whenever the id of the payload item cannot be found in the database.