Furry / 2captcha

A wrapper around the 2captcha api
71 stars 23 forks source link

TypeError: Cannot destructure property 'data' of '(intermediate value)' as it is undefined. #12

Closed stry-rbx closed 2 years ago

stry-rbx commented 2 years ago

For me, this error occurs every time a captcha (funcaptcha) is unsolvable.

const { data } = await solver.funCaptcha("public key here", "page url", "service url")
            ^

TypeError: Cannot destructure property 'data' of '(intermediate value)' as it is undefined.
    at Client.<anonymous> (C:\Users\USERNAME\Desktop\test\index.js:44:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Here is a code snippet (this is in an async function). Public key, page url, and service url are all correct.

const { data } = await solver.funCaptcha("public key", "page url", "service url")
    .catch(err => {
       return console.log(err);
    });

The process then exits.

Furry commented 2 years ago

Hiya, @itztacotimee, thanks for making the issue! I believe this to be a problem with the code you provided, though.

A .catch statement will redefine the result of the promise in the event that the asynchronous function rejects an error. Take a look at this example,

(async () => {
    const { data } = await new Promise((resolve, reject) => reject())
    .catch(err => {
        return { data: "There was an error!" }
    })
    console.log(data);
})();

I return an object in the catch statement that has the data property, and then that object is then destructured. In your code, you have a catch statement and return console.log(err); which is undefined, so when your const { data } = // . . . attempts to destructure it, it throws undefined. Hence the text, "Cannot destructure property 'data' of '(intermediate value)' as it is undefined."

If you have any other issues, or believe this not to be the case, feel free to ask or reopen the issue!

SHIVAM-lab commented 1 year ago

const { data } = await solver.funCaptcha("public key", "page url", "service url").then(resp=>{ return resp ? resp : {}; })

this would work.