cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CAP; UPDATE method doesn't trigger READ hanlder after function _readAfterWrite

alicegavanelli
Participant
4,441

Hallo Experts, I have an issue about READ handler.

I have an Entity with calculation fields. In order to set these fields, I have implemented READ handler.

Triggering a GET request everythings is ok: fields are calculated.

Triggering a PUT request in order to modify entity’s data, fields are empty.


CAP after update runs _readAfterWrite function; this function re-loads entity’s data from DB correctly. After this function, CAP should trigger my READ handler, but it doesn’t.

As a result my calculated fields are empty.

Any ideas on how to solve this problem?

Best regards.

Alice

View Entire Topic
alicegavanelli
Participant

Thank you Sebastian Van Syckel,

the right answer is:

/*
 * srv/raw-service.js
 */
const cds = require('@sap/cds')

module.exports = async function() {
  const { ReadAfterWrite } = this.entities //> ReadAfterWriteService.ReadAfterWrite
  const DatabaseService = await cds.connect.to('db')
  // register for ReadAfterWriteService.ReadAfterWrite as this is the target of the SELECT query
  DatabaseService.after('each', ReadAfterWrite, (row) => { //> invoked for each row of a READ result
    row.text2 = 'virtual text'
  })
}