1

I am building a blockchain project in Substrate. I have created a pallet named carpooling and defined some dispatch functions in the pallet. Now I want to call the dispatch function named book-ride from a polkadot js API. Can anyone suggest to me how can I achieve that?

// Import
const { ApiPromise, WsProvider } = require('@polkadot/api');

async function main(){


    // Construct
    const wsProvider = new WsProvider('ws://127.0.0.1:9944');

    const api = await ApiPromise.create({ provider: wsProvider,
        types: {
            DriverOf: {
                id: 'u32',
                car_no: 'Hash',
                location: ('u32', 'u32'),
                price: 'u32',
            },
            CustomerOf: {
                id: 'u32',
                name: 'Hash',
                location: ('u32', 'u32'),
            },
          }
    });
    
    api.tx.carpooling.book_ride(12,45).then(() => {
        console.log("success");
    }).catch(console.error);


}

main().then(() => console.log('completed'));

I made a node js app to run this and when I run the node index.js command I get the following error.

(node:21899) UnhandledPromiseRejectionWarning: TypeError: api.tx.carpooling.book_ride is not a function
    at main (/home/knoldus/Carpooling-Chain/app/src/index.js:26:23)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21899) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Aman Verma
  • 133
  • 8
  • yeah, handle rejections using `.catch` if using `.then` - or `try/catch` if using `async/await` – Bravo Aug 12 '21 at 07:07
  • note: your `main` function will return before `api.tx.carpooling.book_ride(12,45)` completes, is that intentional? (when you fix the issue that it's not a funciton, of course) – Bravo Aug 12 '21 at 07:09
  • The issue got resolved by using try/catch block but now i get the following error - TypeError: api.tx.carpooling.book_ride is not a function – Aman Verma Aug 12 '21 at 07:35
  • yep, that was always the root cause of the error, as shown in the error message you posted. The issue is that `api.tx.carpooling.book_ride` is not a function - the solution is, determine if it should be a function, if so, why isn't it one, if not, don't use it as one – Bravo Aug 12 '21 at 07:36
  • clearly `api` is whatever `ApiPromise.create` returns ... clearly it also has the properties `.tx.carpooling` ... but does `api.tx.carpooling` have a property called `book_ride` ... and is it a function - that's where console.log's can help you troubleshoot – Bravo Aug 12 '21 at 07:39
  • Thanks, Bravo, It solved the problem that I was struggling with for days. – Aman Verma Aug 12 '21 at 08:35
  • 1
    If you found a solution, please make an answer to your own post so others may learn from it too. – Shawn Tabrizi Aug 12 '21 at 09:38

0 Answers0