Showing results for 
Search instead for 
Did you mean: 

Insert statement not working NodeJs CAPM for POST request

0 Kudos

This is my data-model for Book table

CREATE request for Book table

Here I want to auto increment BooksId without passing it in postman, I can see maxId in console but not inserting in table.

BooksId is null,

I want to auto increment BooksId in Book table through JS and dont want to pass through postman, I am beginner in CAPM please correct where I am doing wrong.

Accepted Solutions (1)

Accepted Solutions (1)


Hi Amit,

In general the code you have posted is correct, but you are being deceived by the default behavior of the CREATE event. You created a new object on line 117 with which you are only writing to the console. When you call the INSERT into Books you are not sending the object to the INSERT statement. So that INSERT is sending nothing to the database. After your handler has finished the default CREATE behavior will take over again and will insert the object into the database.

If you would just assign MaxId to it would be inserted by the default CREATE behavior. = (await cds.tx(req).run('Max(BooksId) as Max'))) + 1

There is an example on how to do this as part of the Bookshop example project:

Answers (0)