ARTICLE AD BOX
Im using megaJS 0.17.2 in my nodeJS program, but it seems like the root object is not being added to the storage (megaDB) object as a property, as seen in the following code clip of my initMega() function:
async function initMega(){ const { Storage } = require('megajs'); try { let megaDB = new Storage({ email: process.env.MEGA_EMAIL, password: process.env.MEGA_PASSWORD }); megaDB.on('ready', () => { console.log("MEGA filesystem loaded."); console.log(Object.keys(megaDB)); //As you can see, it is run in the 'ready' event, so it should display inbox trash and root among others //root = megaDB.root; The root property is broken, so this line is also broken. }); console.log("MEGA connected"); return megaDB; } catch (err) { console.error("Mega failure: " + err); setTimeout(initMega, 3000); } }Does anyone know why the root property isn't appearing?
the issue is that you're returning megaDB before it's ready, and the ready event fires asynchronously after the storage has fully loaded. In megaJS 0.17.2, you need to explicitly call megaDB.ready (a promise) or wait for the event before the root is populated.
async function initMega(){ const { Storage } = require('megajs'); try { let megaDB = new Storage({ email: process.env.MEGA_EMAIL, password: process.env.MEGA_PASSWORD, autologin: false // Prevent auto-login so we control the flow }); // Explicitly trigger login and wait for it await megaDB.login(); console.log("MEGA filesystem loaded."); console.log(Object.keys(megaDB)); // root, inbox, trash should now appear console.log("Root:", megaDB.root); // Should work correctly now console.log("MEGA connected"); return megaDB; } catch (err) { console.error("Mega failure: " + err); setTimeout(initMega, 3000); } }Explore related questions
See similar questions with these tags.
