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

usage of cds.foreach in typescript

akuller
Participant
0 Kudos
350

Hi,

does anyone have a working example of cds.foreach? I always get error messages that it is a query3 and not query2.

 

Docu: https://cap.cloud.sap/docs/node.js/core-services#srv-foreach-entity

Accepted Solutions (1)

Accepted Solutions (1)

akuller
Participant
0 Kudos

The solution:

 

cds.foreach(cds.parse.cql("SELECT ID FROM db_table"), (each: table) => console.log(each.ID))

 

or pass a CQN object. Locks are currently not supported, see https://github.com/cap-js/cds-types/issues/331 & https://influence.sap.com/sap/ino/#/idea/335478/

Answers (2)

Answers (2)

catano
Active Participant
0 Kudos

Hi @akuller ,

Is it possible that maybe you missed a single quote (') around your entity name?

Could you try it in the following way:

 

SELECT.from('Requests')

 

Regards,
Peter 

akuller
Participant
0 Kudos
Hi @catano I get the same error when I change it to ‘db_Requests’. Requests comes from the cds typer. Does it work for you?
catano
Active Participant
0 Kudos

Hi @akuller , it works for me. I just tried with the Standalone CDI with @cap-js/cds-typer 0.4.0. I receive no type error and no runtime error, foreach just runs with no problem.

akuller
Participant
0 Kudos

Hi @catano 

The query is based on the example from the docu: https://cap.cloud.sap/docs/node.js/core-services#srv-foreach-entity

akuller_q_0-1730708979869.png

@cap-js/asyncapi: 1.0.2
@cap-js/cds-typer: 0.28.0
@cap-js/cds-types: 0.7.0
@cap-js/db-service: 1.14.1
@cap-js/hana: 1.4.0
@cap-js/openapi: 1.0.7
@cap-js/sqlite: 1.7.5
@cap-js/telemetry: 1.0.1
@sap/cds: 8.4.0
@sap/cds-compiler: 5.4.0
@sap/cds-dk: 8.4.0
@sap/cds-dk (global): 8.3.0
@sap/cds-fiori: 1.2.7
@sap/cds-foss: 5.0.1
@sap/cds-mtxs: 2.3.0
@sap/eslint-plugin-cds: 3.1.1
Node.js: v20.12.0