dijs / wiki

Wikipedia Interface for Node.js
MIT License
313 stars 62 forks source link

page.mainImage() throws error 'Cannot read property 'imageinfo' of undefined' #53

Closed kper closed 7 years ago

kper commented 7 years ago

I'm getting the same error like in Issue #51. However, I'm using the english wiki page and the page has an infobox with the File prefix.

wiki()
.page("FC Copenhagen")
.then(page => page.mainImage())
.then(console.log)
.catch(e => console.error(e));

RawImage:

 { pageid: 28199557,
    ns: 6,
    title: 'File:FC København.svg',
    imagerepository: 'local',
    imageinfo: [ [Object] ] },
... 

imageinfo:

{ url: 'https://upload.wikimedia.org/wikipedia/en/9/93/FC_K%C3%B8benhavn.svg',
  descriptionurl: 'https://en.wikipedia.org/wiki/File:FC_K%C3%B8benhavn.svg',
  descriptionshorturl: 'https://en.wikipedia.org/w/index.php?curid=28199557' }

Error:

TypeError: Cannot read property 'imageinfo' of undefined
    at node_modules/wikijs/dist/page.js:1:4213
    at process._tickCallback (internal/process/next_tick.js:109:7)
dijs commented 7 years ago

Thanks for the reproducible error case. I will try to get to this fix soon.

dijs commented 7 years ago

I wonder if FC København.svg being different than FC_K%C3%B8benhavn.svg is the issue. If you want, you can try changing the conditional of byNamedImage to use URL encoding for equality.

kper commented 7 years ago

I think the problem is when the lib tries to find the main image. For debugging purposes, I added some console.log to your byNamedImage function and found this mismatch (see the last two lines of the output) between name and searchName.

Modified Code:

const byNamedImage = searchName => image => {
    const [, name] = image.title.split(':');
    console.log(image);
    console.log("Name: " + name);
    console.log("searchName: " + searchName);

    return name === searchName;
};

Output:

{ pageid: 28199557,
  ns: 6,
  title: 'File:FC København.svg',
  imagerepository: 'local',
  imageinfo: 
   [ { url: 'https://upload.wikimedia.org/wikipedia/en/9/93/FC_K%C3%B8benhavn.svg',
       descriptionurl: 'https://en.wikipedia.org/wiki/File:FC_K%C3%B8benhavn.svg',
       descriptionshorturl: 'https://en.wikipedia.org/w/index.php?curid=28199557' } ] }
Name: FC København.svg
searchName: frameless|Logo
...

PS:

I didn't try the URL encoding yet

dijs commented 7 years ago

Good debugging. Yep sure enough... The main image is being parsed wrong.

[[File:FC København.svg|frameless|Logo]] is parsed as frameless|Logo instead of File:FC København.svg

dijs commented 7 years ago

Fixed in v4.0.0

hzitoun commented 6 years ago

Still having the same problem if with version 4.3.1. (node:2016) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read prop erty 'imageinfo' of undefined

dijs commented 6 years ago

Can you please provide more information, a code example please?

hzitoun commented 6 years ago

@dijs yes I've created a new issue https://github.com/dijs/wiki/issues/62.

dijs commented 6 years ago

Thanks, fixed.