-4

Hello i'm getting this error when trying to access values from a function.

node:events:505
      throw er; // Unhandled 'error' event
      ^

TypeError: createInvoice is not a function or its return value is not iterable
    at Client.<anonymous> (C:\Users\Utente\Desktop\sito\InvoiceBot\index.js:101:63)
    at Client.emit (node:events:527:28)
    at InteractionCreateAction.handle (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)
    at Object.module.exports [as INTERACTION_CREATE] (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:354:31)
    at WebSocketManager.<anonymous> (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:238:12)
    at WebSocketManager.emit (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
    at WebSocketShard.<anonymous> (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\@discordjs\ws\dist\index.js:1103:51)
    at WebSocketShard.emit (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\@vladfrangu\async_event_emitter\dist\index.js:282:31)
    at WebSocketShard.onMessage (C:\Users\Utente\Desktop\sito\InvoiceBot\node_modules\@discordjs\ws\dist\index.js:938:14)
Emitted 'error' event on Client instance at:
    at emitUnhandledRejectionOrErr (node:events:384:10)
    at processTicksAndRejections (node:internal/process/task_queues:85:21)

C:\Users\Utente\Desktop\sito\InvoiceBot>

This is the function where i'm returning from:

function createInvoice(item_name, note_message, quantity, cost, payer_email){ 

    let create_invoice_json = {
        "merchant_info": {
            "email": config.emailAddress,
            "business_name": config.business_name,
        },
        "billing_info": [
        {
        "email": payer_email,
        }
        ],
        "items": [
            {
                "name": item_name,
                "quantity": quantity,
                "unit_price": {
                    "currency": "USD",
                    "value": cost,
                }
            },
        ],
        
        "note": note_message,
        }

    paypal.invoice.create(create_invoice_json, function (error, invoice) {
        if (error) {
            throw error;
        } else {
            //console.log(invoice)
            return [invoice.id, invoice.status, invoice.billing_info[0],  invoice.items[0], invoice.items[1], invoice.invoice_date, invoice.total_amount[1]] 
        }
    });
}

And this is how i want to access the values:

 const [id, status, payer, item, q, date, price] = createInvoice(item_name, note_message, quantity, cost, payer_email)

I think it should be correct but its returning me that error...

KaoDev
  • 1
  • 2
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/is_not_iterable – CBroe Jul 26 '23 at 09:39
  • 2
    Have you checked what your `createInvoice` call actually returns? The `return` statement is inside of the callback function you are passing to `paypal.invoice.create`, so I very much doubt that it even makes it's way further up. – CBroe Jul 26 '23 at 09:41
  • the return is okay, its just getting the value that gives an error – KaoDev Jul 26 '23 at 09:46
  • Okay, then see the explanation I linked to. A mere plain object, is not an _iterable_ object. – CBroe Jul 26 '23 at 09:49
  • What is `console.log(createInvoice(item_name, note_message, quantity, cost, payer_email))` looking alike at your end? You do not need to share sensitive data here, if you have anything sensitive in there, just replace it with a dummy value. – Lajos Arpad Jul 26 '23 at 09:51
  • its actually undefined... i just noticed that, then the problem is in the return because the values are not undefined. But why is it undefined the return statement seems correct to me – KaoDev Jul 26 '23 at 10:27
  • 1
    @KaoDev your return statement is inside the callback function passed to `paypal.invoice.create` and it's not a return statement of `createInvoice`. Written an answer to elaborate on that. – Lajos Arpad Jul 26 '23 at 11:04
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – CherryDT Jul 26 '23 at 11:05

1 Answers1

1

createInvoice returns undefined, because the return statement that we see physically inside of it actually returns values of the callback function passed to paypal.invoice.create. You will need to either return something meaningful from createInvoice proper, or move the logic that needs the values into the callback.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • wdym? how should i do that practically? – KaoDev Jul 26 '23 at 13:25
  • @KaoDev you can 1. Create an empty array just before you call `paypal.invoice.create(...)`, I will call it `ret`, 2. put an `await` keyword in the line of your `paypal.invoice.create(...)` call, 3. Change the `return` statement to `ret = return [invoice.id, invoice.status, invoice.billing_info[0], invoice.items[0], invoice.items[1], invoice.invoice_date, invoice.total_amount[1]];` and 4. at the very end of your `createInvoice` function, `return` it as `return ret;` – Lajos Arpad Jul 26 '23 at 13:40
  • i tried that and it returned an empty array, though i solved my problem by doing what i wanted in the callback function! Thanks for helping :D – KaoDev Jul 26 '23 at 14:31
  • @KaoDev happy to help. If the answer led you to the solution, then you might consider accepting it as the correct answer. As about the empty array, it seems that you missed step 3. If you are interested about the reason, you might edit your question with the exact code that you tried and message me here. – Lajos Arpad Jul 26 '23 at 14:35