scttcper / deluge

Deluge API wrapper

Home Page:https://deluge.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Cannot read property '0' of undefined at Deluge.addTorrent

abdatta opened this issue · comments

(node:4347) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '0' of undefined
    at Deluge.addTorrent (/home/yakubo/nyaa-dl/node_modules/@ctrl/deluge/src/index.ts:272:27)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:94:5)
(node:4347) 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 ha)
(node:4347) [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.

I get this error when trying to add a new torrent. I see the fault lies in this line:

    const res = await this.request<AddTorrentResponse>('web.add_torrents', [[{ path, options }]]);

    if (res.body.result[0][0] === false) {
      throw new Error('Failed to add torrent');
    }

It cannot find [0] of res.body.result[0] cuz it is already undefined. So, I checked the output of this response manually and I found res.body.result returns to be a boolean value for this request. The following is a response I found when manually making the request.

{"id": 3841, "result": null, "error": null}

So, I am fixing this in my PR for the issue #69 by replacing the above code with the following:

    const res = await this.request<AddTorrentResponse>('web.add_torrents', [[{ path, options }]]);

    if (res.body.result === false) {
      throw new Error('Failed to add torrent');
    }

Thanks for reporting and making a PR!