The addBook function modifies the state of the contract, which means that the transaction calling that function will be broadcasted through the network, pay gas, and will be mined and included in a block. All this will be asynchronous, so, you will not get the response right away.
You will need to emit an event containing the data that you need and then subscribe to that event.
For example:
event BookAdded(uint bookId);
function addBook(string memory _name, bool _promoted) external returns (uint) {
uint pdate = block.timestamp;
uint bookId = bookList.length;
bookList.push(Book(bookId, _name, 0, pdate, _promoted));
bookToOwner[bookId] = msg.sender;
emit BookAdded(apbookIdpId);
//return the id of the created book
return apbookIdpId;
}
And subscribe to it like:
const subscription = web3.eth.subscribe('logs', {
address: '0x123456..', // The smart contract address
topics: ['0x12345...'] // The topics of your event
}, function(error, result){
if (!error)
console.log(result);
});
I answered a similar question yesterday with details. For more details, check this answer: How can I get transaction result?
apbookIdpId;? that wouldn't compile – sola24 Sep 15 '22 at 21:00