Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assign to read only property 'random' #38

Closed
JacenBoy opened this issue Dec 19, 2019 · 5 comments
Closed

Cannot assign to read only property 'random' #38

JacenBoy opened this issue Dec 19, 2019 · 5 comments
Assignees
Labels

Comments

@JacenBoy
Copy link

After updating from 2.0.3 to 2.0.4, I receive an error whenever trying to do a search. Tested with both Gelbooru and Safebooru and both throw the same error. Issue is resolved upon rolling back to 2.0.3.

[2019-12-18 22:18:49]: ERROR Unhandled rejection: TypeError: Cannot assign to read only property 'random' of object '[object Object]'

Code below is from the discord.js project that I noticed the issue in.

  const booru = require("booru");
  var site = (! ["dm","group"].includes(message.channel.type) ? (message.channel.nsfw ? "gb" : "sb") : "sb");
  var taglist = args.join(" ");
  var tagarray = taglist.split(", ");
  for (var i=0;i<tagarray.length;i++) {
    tagarray[i] = tagarray[i].replace(/\s/g, "_");
  }
  if (site == "gb") tagarray.push("-webm", "-mp4");
  var img = await booru.search(site, tagarray, {limit: 1, random: true});
  if (!img[0]) {
    message.channel.send("No results found.");
    client.logger.warn(`No results found for tags: ${tagarray.join(", ")}`);
    return;
  }
  const embed = {"embed": {
    "title": `${site == "gb" ? "Gelbooru" : "Safebooru"} #${img[0].id}`,
    "url": `https://${site == "sb" ? "safebooru.org" : "gelbooru.com"}/index.php?page=post&s=view&id=${img[0].id}`,
    "color": client.colorInt(site == "sb" ? "#84a8b9" : "#006ffa"),
    "image": {"url": img[0].file_url},
    "footer": {"text":`Score: ${img[0].score}`}
  }};
  client.logger.log(`${site == "gb" ? "Gelbooru" : "Safebooru"} #${img[0].id} found for tags: ${tagarray.join(", ")}`);
  message.channel.send(embed);
@AtoraSuunva
Copy link
Owner

I can't reproduce the error using the snippet you posted, do you get a stack trace?

If you don't, can you try using this to catch the unhandled rejection?

process.on('unhandledRejection', (reason, p) => {
  console.error(`Uncaught Promise Error: \n${reason}\nStack:\n${reason.stack}\nPromise:\n${require('util').inspect(p, { depth: 2 })}`)
})

random is only assigned in Site.ts, but that wasn't touched in 2.0.4

@AtoraSuunva AtoraSuunva self-assigned this Dec 19, 2019
@JacenBoy
Copy link
Author

Here's the stack trace

TypeError: Cannot assign to read only property 'random' of object '[object Object]'
Stack:
TypeError: Cannot assign to read only property 'random' of object '[object Object]'
    at Object.<anonymous> (C:\Users\jacen\Documents\GitHub\michelle\node_modules\booru\dist\structures\SearchResults.js:1:1161)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (C:\Users\jacen\Documents\GitHub\michelle\node_modules\booru\dist\boorus\Booru.js:1:375)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
Promise:
Promise {
  <rejected> TypeError: Cannot assign to read only property 'random' of object '[object Object]'
      at Object.<anonymous> (C:\Users\jacen\Documents\GitHub\michelle\node_modules\booru\dist\structures\SearchResults.js:1:1161)
      at Module._compile (internal/modules/cjs/loader.js:778:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Module.require (internal/modules/cjs/loader.js:692:17)
      at require (internal/modules/cjs/helpers.js:25:18)
      at Object.<anonymous> (C:\Users\jacen\Documents\GitHub\michelle\node_modules\booru\dist\boorus\Booru.js:1:375)
      at Module._compile (internal/modules/cjs/loader.js:778:30) }```

@JacenBoy
Copy link
Author

Sorry, forgot to mention I'm on Node v10.16.2 and NPM v6.9.0

@AtoraSuunva
Copy link
Owner

Can't reproduce it on Node v13.5.0 or Node v10.16.2, are you adding onto Array.prototype an Array.prototype.random method?

I can reproduce the same error by using:

Object.defineProperty(Array.prototype, 'random', {
  value: () => {},
  writable: false,
});

AtoraSuunva pushed a commit that referenced this issue Dec 22, 2019
Fix prototype assign issue if Array.prototype was written to
@AtoraSuunva
Copy link
Owner

Latest version (2.0.5) fixes this issue https://www.npmjs.com/package/booru/v/2.0.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants