qgustavor / mega

Unofficial JavaScript SDK for MEGA
https://mega.js.org/
MIT License
160 stars 42 forks source link

.upload() is not a function? #136

Closed AliAryanTech closed 1 year ago

AliAryanTech commented 1 year ago

Describe the bug Well, I'm trying to test .upload but the terminal saying it's not a function? even I tried all examples of .upload it's saying it's not a function. also storage.once(), saying the same it's not a function.

To Reproduce

const { Storage } = require('megajs')

const storage = new Storage({
  email: '****', // email address 
  password: '*****' // my password 
}, error => {
  return console.log(`${error ?  "Some error happened" : "User is now logged in"}`)
}).ready 

storage.upload('hello-world.txt', 'Hello world!', (error, file) => {
  if (error) return console.error('There was an error:', error)
  console.log('The file was uploaded!', file)
})

Expected behavior I tried in termux & replit it test the code! Termux termux screenshot

Replit: replit screenshot

Additional context One more thing can you tell me an example with a Buffer? Like how to upload a Buffer, I saw those example but coz of upload function unable to test upload by buffer, that's why I thought it would be better if you tell me an example!!

let buffer = await getBuffer("https://telegra.ph/file/e991bb4b535a0f1425aa0.jpg")

const file = await storage.upload('hello-world.txt', Buffer.from(buffer, 'base64')).complete
console.log('The file was uploaded!', file)

Like this? That's just a buffer, I don't know what's the file name and size!? Then why this, allowUploadBuffering: true

qgustavor commented 1 year ago

I'm quite confused on what you're trying to do, I don't know your JavaScript background, but seems you are mistaking how asynchronous JavaScript works.

Do you know how to use Promises? And how about callbacks? If not check some tutorial for those topics first.

storage.ready is a Promise, so seems you're missing an await keyword, but at the same time you defined a callback at the same time, which… is weird. Use one or another. Callbacks is the old pattern, promises is the new one, this library support both because backyards compatibility but I recommend learning and using promises as they avoid some issues that callbacks have (like nesting hell).

Seems you are using Node CJS, it does not support top level await, check examples in mega.js.org to workaround this limitation (use a async IIFE). If possible learn and use ESM, it does not have this limitation.

Try learning about async patterns in JavaScript, them fix the login first, that's a lot for now, I will answer the upload question later.

I'm on the mobile client, when I get home I will move this into a discussion because that's not an issue with the library.