0

I'm currently having a trouble to understand how to access the fileSize variable to use in another async function. I need the value of fileSize in order to upload a media. Below here is the code containing the fileSize variable.

async function getMedia(url, auth) {

const getImage = {
    url: url,
    oauth: auth,
};
var downloadImage = get(getImage).then(function(response) {
    let imageBuffer = Buffer.from(response.body);
    let imageBase64 = imageBuffer.toString('base64');
    let decodedBase64 = atob(imageBase64);
    let fileSize = decodedBase64.length;
    //console.log(fileSize);
});
await downloadImage;
};

I want to access it in another async function, like this

async function uploader() {

const uploadImageInit = {
    url: 'https://upload.twitter.com/1.1/media/upload.json',
    command: 'INIT',
    total_bytes: fileSize, // access it here
    media_type: 'image/jpeg'
};

I tried using it like this

getMedia().then((response) => console.log(response.fileSize));

But it only return an undefined. Any idea how to do it correctly?

nawhki
  • 57
  • 6
  • what library is get imported from `get()`? – Mosia Thabo Aug 28 '20 at 09:28
  • just return the value in your `then` function, and await that value – Derek Aug 28 '20 at 09:29
  • Does this answer your question? [How to get data returned from fetch() promise?](https://stackoverflow.com/questions/47604040/how-to-get-data-returned-from-fetch-promise) – Mosia Thabo Aug 28 '20 at 09:32
  • the library imported is request, using const get = util.promisify(request.get); @MosiaThabo – nawhki Aug 28 '20 at 09:35
  • Sorry man, I've been a bit tied up and couldn't reponse to this. Ok thanks for mentioning that library. Also note that `getMedia()` is defined to receive parameters and you didn't provide them when you called it. I have provided an answer below, I didn't test this, please do since you have the endpoints on your side. – Mosia Thabo Aug 28 '20 at 09:54

1 Answers1

1

Firstly, you did not return anything within then(callback) so nothing is returned. You instead have created scoped variables within that callback which are not used at all.

var downloadImage = get(getImage).then(function(response) {
    let imageBuffer = Buffer.from(response.body);
    let imageBase64 = imageBuffer.toString('base64');
    let decodedBase64 = atob(imageBase64);
    let fileSize = decodedBase64.length;
    //console.log(fileSize);
});

All those block variables are not used at all. So I would suggest you do the following:

let image = {};

async function getMedia(url, auth) {
  const getImage = {
    url: url,
    oauth: auth,
  };
  return await get(getImage).then(function(response) {
    return response.json().then(data => {
      return data;
    }).catch(error=> error);
  });
};

// You will have to call getMedia before uploader

getMedia(someUrl, someAth).then(response=>{
  console.log(response);
  //here your response is the actual image.
  image = { 
    imageBuffer : Buffer.from(response.body,
    imageBase64 : imageBuffer.toString('base64'),
    decodedBase64 : atob(imageBase64),
    fileSize : decodedBase64.length,
  }
});

async function uploader() {

  const uploadImageInit = {
      url: 'https://upload.twitter.com/1.1/media/upload.json',
      command: 'INIT',
      total_bytes: image.fileSize, // get access to image.fileSize
      media_type: 'image/jpeg'
  };
}
Mosia Thabo
  • 4,009
  • 1
  • 14
  • 24