0

so this is a weird thing.

I am running a puppeteer test and everything works when I console.log the count.

 console.log((await page.$$('.list-view > ul > .selected > h5 > .msg-title')).length);

However, when I try to wrap the count in a variable or const, it returns undefined.

let test = await page.$$('.list-view > ul > .selected > h5 > .msg-title').length
console.log(test)

results:

undefined. 

id like to eventually write this in a json format to save it to a DB.

chewie
  • 529
  • 4
  • 17
  • It's becouse it's an await function... It console.logs() before the result comes back from the db. – Laczkó Örs Mar 06 '21 at 13:18
  • 2
    `let test = await page.$$('.list-view > ul > .selected > h5 > .msg-title').length` -> `let test = (await page.$$('.list-view > ul > .selected > h5 > .msg-title')).length` otherwise you try to essentially `await promise.length` – VLAZ Mar 06 '21 at 13:18
  • 2
    It's a precedence issue. You need to put `()` around `await page.$$(...)` prior to `.length`. Otherwise you're doing `page.$$(...).length` and then `await`ing that. Promises don't have a `length` property. :-) (And no, this wasn't obvious. It bites everyone at one time or another.) – T.J. Crowder Mar 06 '21 at 13:18
  • @BudaÖrs - No, that's not the reason (although it's true that nothing waits for the promise to settle). (Also, `await` isn't a function. :-) ) – T.J. Crowder Mar 06 '21 at 13:30
  • Yeah, you are right:) I didn't say tho that `await` is a function :-) – Laczkó Örs Mar 06 '21 at 14:29

0 Answers0