cancel
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)

bob_den_os
Explorer

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 req.data object into the database.

If you would just assign MaxId to req.data.BooksId it would be inserted by the default CREATE behavior.

req.data.BooksId = (await cds.tx(req).run(SELECT.one.from(req.target).columns('Max(BooksId) as Max'))) + 1

There is an example on how to do this as part of the Bookshop example project:
https://github.com/SAP-samples/cloud-cap-samples/blob/main/bookshop/srv/admin-service.js#L11

Answers (0)