My code like this:
public async update(params: any) {
await Database.transaction(async (trx) => {
const data = await Period.findOrFail(params.id, { client: trx })
data.from = params.from
data.to = params.to
data.desc = params.desc
data.useTransaction(trx)
await data.save()
let arrFiles = []
for (let i in params.file_names) {
arrFiles.push({
fileName: Date.now() + '_' + params.file_names[i],
fileUri: params.file_uris[i],
createdBy: params.created_by,
})
}
await period
.related('files')
.updateOrCreateMany(arrFiles, 'fileUrl', { client: trx })
await File.query()
.whereIn('id', params.removed_file_id)
.useTransaction(trx)
.delete()
return { success: true }
})
}
I had trx
in findOrFail
, save
, updateOrCreateMany
, delete
. But if I run the query it's not read return { success: true }
Is there something wrong with adding trx
to the update process?
If I check on the preview tab in the console, itu return empty object, no properties. Whereas the update prosess success. I check in table of database, it success create, delete & update. Should it return { success: true }
How can I solve this problem?
Please help. Thanks
Note: I follow this reference:: https://docs.adonisjs.com/guides/database/transactions
Your function is a void function so it does nothing. Add a return statement will solve it.
public async update(params: any) { return Database.transaction(async (trx) => { const data = await Period.findOrFail(params.id, { client: trx }) data.from = params.from data.to = params.to data.desc = params.desc data.useTransaction(trx) await data.save() let arrFiles = [] for (let i in params.file_names) { arrFiles.push({ fileName: Date.now() + '_' + params.file_names[i], fileUri: params.file_uris[i], createdBy: params.created_by, }) } await period.related('files').updateOrCreateMany(arrFiles, 'fileUrl', { client: trx }) await File.query().whereIn('id', params.removed_file_id).useTransaction(trx).delete() return { success: true } }) }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.