0

So, I have this code

(async function checkPrinterConnection() {
  (await window.electronAPI.checkPrinterConnection() == undefined) ?
   (document.getElementById('printerIcon').classList.add('printer-icon-error'),
   document.getElementById('printerIcon').title = "Impresora sin conexión",
   document.getElementById('refreshPrinter').innerHTML = 'Reintentar impresora')
   :
   (document.getElementById('printerIcon').classList.remove('printer-icon-error'),
   document.getElementById('printerIcon').title = "Impresora conectada",
   document.getElementById('refreshPrinter').innerHTML = '')
})()

that works well, but when I change it to this one below it returns "Uncaught TypeError: document.getElementById(...) is not a function" and I don't understand why

const printerIcon = document.getElementById('printerIcon')
const refreshPrinter = document.getElementById('refreshPrinter')
(async function checkPrinterConnection() {
  (await window.electronAPI.checkPrinterConnection() == undefined) ?
   (printerIcon.classList.add('printer-icon-error'),
   printerIcon.title = "Impresora sin conexión",
   refreshPrinter.innerHTML = 'Reintentar impresora')
   :
   (printerIcon.classList.remove('printer-icon-error'),
   printerIcon.title = "Impresora conectada",
   refreshPrinter.innerHTML = '')
})()
fermin
  • 11
  • 1
  • 2
  • 2
    The const lines are probably running before the page is rendered. – Hogan Aug 25 '22 at 12:18
  • what happens if you console.log(document) at first line of second code block? – ALWebDev Aug 25 '22 at 12:19
  • 2
    Are you purposefully making your code harder to read? If, so they you might consider using a code minifier/uglifier. No need to do it yourself. If not, then don't abuse the conditional operator for this. – Ivar Aug 25 '22 at 12:20
  • In a browser, `window.document` is never undefined. But maybe Electron is different. You might want to add the appropriate tags. – Thomas Aug 25 '22 at 12:20
  • I agree with @ivar -- what you are doing here is an if statement. Coding it this way just obscures your intent and does not make it better in any other way – Hogan Aug 25 '22 at 12:24
  • So, looks like the problem was a missing ';' at the end of 'document.getElementById('printerIcon')' and document.getElementById('refreshPrinter'), sorry everyone, it was a dumb question – fermin Aug 25 '22 at 12:25
  • @fermin You just learned first hand why it is always recommended to use semicolons in JavaScript even when they're not always required. – Ivar Aug 25 '22 at 12:28
  • @fermin: In fairness, JavaScript's automatic semicolon insertion is a dumb feature that exists solely to lull folks into a sense of complacency (it works so often!) just to burn them with non-obvious errors when they get unlucky. Sadly not so dumb a question. – ShadowRanger Aug 25 '22 at 12:28
  • @Ivar, I’d say that semicolon auto insert does not trouble most of the times, except when using more complex syntax or abusing, where the coders should already be very much aware of where it fails – Rodrigo Rodrigues Aug 25 '22 at 12:32
  • @RodrigoRodrigues I agree with most of the time, but when you are regularly using IIFE's you'll run into them sooner or later and the errors are not always straight forward. Most linters and code conventions mandate the use of semicolons. – Ivar Aug 25 '22 at 12:35

0 Answers0