kanasimi / wikiapi

JavaScript MediaWiki API for node.js
https://kanasimi.github.io/wikiapi/
BSD 3-Clause "New" or "Revised" License
50 stars 6 forks source link

TypeError: Cannot read properties of undefined (reading 'id') #69

Closed hugolpz closed 10 months ago

hugolpz commented 11 months ago

Hello Kanasimi,

I continue to fight but I still hit some walls.

I have a upload files call such as :


(async () => {
    // Connect
    const wiki = new Wikiapi;
    await wiki.login(USER, PASS, API);

/* *************************************************************** */
/* CORE ACTION(S) HERE : HACK ME ! ******************************* */
    // Set upload parameters, maily for licensing reasons.
    // Note: parameter `text`, filled with the right wikicode `{{description|}}`, can replace most parameters.
    let options = {
        author: `[[User:Yug]]`,
        date: new Date().toISOString().split('T')[0],
        //source_url: 'https://github.com/kanasimi/wikiapi',
        permission: '{{own}}',
        other_versions: '',
        other_fields: '',
        license: ['{{self|cc-by-sa-4.0}}' ],
        categories: [`[[Category:WikiVoc]]`, `[[Category:Yug test: upload]]` ],
        bot: 1
    };
    console.log(words[0])
    for(i=0;i<words.length;i++){
        console.log(words[i].filename)
        // Upload file from URL
        try{
            wiki.upload({
                file_path: `../output/${words[i].filename}`,
                filename: `${words[i].filename}`,  // default : keep filename
                comment: `Upload children-friendly, colorful illustration for ${words[i].word}.`,
                ignorewarnings: 1,  // overwrite
                ...options,
                description: `Children-friendly illustration of the word ''« ${words[i].word} »'', on white background.`,
                author: `[[User:Yug]]`,
            });
        }catch(e){ console.error('Fails upload: '+ e); }
    }
/* END CORE ****************************************************** */
/* *************************************************************** */

})();

And I get the error :

yug@yugpc2023:~/Documents/communal/WikiVoc/Dragons_Bot$ node wiki-upload-many.js
get_API_parameters: Cache information about the API modules of commonswiki: module path=query+userinfo 
get_API_parameters: Cache information about the API modules of commonswiki: module path=query+siteinfo 
Username Dragons Bot is connected !
{ word: 'angle', filename: 'WikiVoc—angle.jpg' }
WikiVoc—angle.jpg
get_API_parameters: Cache information about the API modules of commonswiki: module path=upload 
/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/task.js:2162
                        } else if (this.last_page.id && ('missing' in this.last_page)
                                                  ^

TypeError: Cannot read properties of undefined (reading 'id')
    at wiki_API.next (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/task.js:2162:30)
    at check_structured_data (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/edit.js:1348:12)
    at upload_callback (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/edit.js:1429:38)
    at check_session_badtoken (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/query.js:208:4)
    at XMLHttp_handler (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/wiki/query.js:739:4)
    at IncomingMessage.<anonymous> (/home/yug/Documents/communal/WikiVoc/Dragons_Bot/node_modules/cejs/application/net/Ajax.js:2318:6)
    at IncomingMessage.emit (node:events:525:35)
    at endReadableNT (node:internal/streams/readable:1359:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Node.js v18.13.0

Any ideas ?

kanasimi commented 11 months ago

@hugolpz I think I might have to try uploading files. If I finish the test, can you delete the test files?

hugolpz commented 11 months ago

Important : for me, the same error pops up for Wikimédia Commons (bot or user's account), and Lingualibre.org After a year away I'm rusty, I may do things wrong somewhere but I don't know. So after fighting unsuccessfully on a single error I report it here. My apologizes for the juniorship and thank you too.

kanasimi commented 11 months ago

I don't get this error in Commons. Maybe it's a version problem? I'll update it and see...

kanasimi commented 11 months ago

If the problem persists, please reopen this issue.

hugolpz commented 10 months ago

Hello @Kanasimi, I super-simplified my code in order to lead a minimalist test, it still stales for upload() to Commons.

Versions

$ node --version
v20.10.0
$ npm list
dragons-bot@0.0.1 /home/yug/Documents/communal/WikiVoc/Dragons_Bot
├── cejs@4.5.7
├── node-fetch@2.7.0
├── npm-check-updates@16.14.12
└── wikiapi@1.19.5

Edits ✅

Dragons_Bot is able to login and edit Commons without issue, see : https://commons.wikimedia.org/wiki/Special:Contributions/Dragons_Bot

Upload ⏸️🚫

But the following wiki.upload() code stales despite all my simplifications :

// PURPOSE: Script to upload targets
// Run: $node wiki-upload-many.js
const Wikiapi= require('wikiapi');
const logins = require('./logins.js');

(async () => {
    // Connect
    const wiki = new Wikiapi;
    await wiki.login({
        language : 'en',
        API_URL : logins.commons.api,
        data_API_URL : logins.commons.api,
        //SPARQL_API_URL : '...'
        user_name: logins.commons.user,
        password: logins.commons.pass,
        //origin: '*', // if commented then connect
    });
    console.log(`Username Dragons Bot is connected !`);

/* *************************************************************** */
/* CORE ACTION(S) HERE : HACK ME ! ******************************* */
    // Set upload parameters, maily for licensing reasons.
    // Note: parameter `text`, filled with the right wikicode `{{description|}}`, can replace most parameters.
    let options = {
        author: `[[User:Dragons Bot|]]`,
        date: new Date().toISOString().split('T')[0],
        description: `Chinese mandarin audio`,
        other_versions: '',
        other_fields: '',
        permission: `{{own}}`,
        categories: [`[[Category:Dragons Bot test: upload]]` ],
        bot: 1,
       // tags:"tag1|tag2",
    };
    for(i=0;i<1;i++){
        try{
            wiki.upload({
                file_path: "./data/Dragons_Bot_test.jpg", // <======================================== CREATE THIS FILE TO TEST
                //source_url: 'https://github.com/kanasimi/wikiapi',
                filename: "Dragons_Bot_Test.jpg",
                comment: `Upload Shtooka recorder file {TITLE} as Lingua Libre item.`,
                ignorewarnings: 1,
                bot: 1,
                license: ['{{cc-by-sa-3.0}}' ],
                //text: 'Wikibot test, wait few hours before {{tl|speedy}}.',
                 ...options,
            });
            /**/
        }catch(e){ console.error('Fails upload: '+ e); }
    }
/* END CORE ****************************************************** */
/* *************************************************************** */

})();

Terminal after 10mins

It runs for 30secs with 4 logs printed then stales . No error.

$ node ./wiki-upload-test.js
get_API_parameters: Cache information about the API modules of commonswiki: module path=query+userinfo 
get_API_parameters: Cache information about the API modules of commonswiki: module path=query+siteinfo 
Username Dragons Bot is connected !
get_API_parameters: Cache information about the API modules of commonswiki: module path=upload 

Test file

You may need an image or file to upload (this one below may already be on commons ) Dragons_Bot_test

Next phases

Any test or code change I may do to help this investigation ?

hugolpz commented 10 months ago

Hello @kanasimi.

Same, stalling, no error, no upload with the JS code above. Upload to Commons does work for you ?

package.json :

  "dependencies": {
    "cejs": "github:kanasimi/cejs",
  //  "npm-check-updates": "latest",
    "wikiapi": "github:kanasimi/wikiapi"
  }

npm list :

$ npm list --depth=1
dragons-bot@0.0.1 /home/yug/Documents/communal/WikiVoc/Dragons_Bot
├── cejs@4.5.7 (git+ssh://git@github.com/kanasimi/cejs.git#0f3d34255e18fbf8df9b9f72b50e073759060dec)
└─┬ wikiapi@1.19.5 (git+ssh://git@github.com/kanasimi/wikiapi.git#02ec0a3a9121a8bbb972b69f81f18b882a0ed37c)
  └── cejs@4.5.7

And my local cejs is the latest cejs 👍🏼 image

kanasimi commented 10 months ago

Sorry I forgot to update npm. please use version 4.5.8.