#I'm a bit lost How to I get the return value to const image?

46 messages · Page 1 of 1 (latest)

ashen surge
#

or How do I get any value to outside the function

#

SHouldn't be enough to return img ?

jade veldt
#

I don't know if that pipe after equal sign is something from your ide or a typo, bu your assigning a function definition to image const, not an image.

#

g!badeyes @ashen surge

delicate starBOT
#

@ashen surge Please do not post pictures of code (especially photos of a physical screen) as they are difficult to read and difficult to correct (as you cannot copy code from a screenshot to modify it).

ashen surge
#

I'm assigning to const image the return ?

#

return img should go to const image

#

or anything else I return right?

jade veldt
#

Please copy paste your code.

#

g!codeblock @ashen surge

delicate starBOT
#

@ashen surge, you can use the following snippet to have your code formatted and colored by Discord. Replace ts with the language you need (i.e. html, js, css, etc)
```ts
// your code goes here
```

ashen surge
#
        if (err)
          console.log(err, err.stack); // an error occurred

        else {
          img = new Buffer(data.MetricWidgetImage).toString('base64');
   
          return img;

        }
      });```
#

I was just testing getting anything out, like :

        if (err)
          console.log(err, err.stack); // an error occurred

        else {

          return "stuff";

        }
      });```
#

"stuff" should go to const image?

#

but it doesnt later when I use it in the code

night wren
#

No, it shouldn't.

jade veldt
#

The coe you pasted is different from the one you posted earlier in image.

#

And you're assigning a function definition to image, not the value it should return.

ashen surge
#

how can I get a value to the outside of the function?

night wren
#

You're assigning what getMetricWidgetImage returns, and we don't know what it is unless you point us to the documentation of that function.

ashen surge
#

and how can I get a value in that else clause to the outside

#

of this function

night wren
#

You probably can't. The function probably expects a callback because it's asynchronous. You can't expect to synchronously get a result from a function which computes the result asynchronously. Like you can't expect to read the answer to an email right after you've sent the email. What's the documentation of that method?

ashen surge
#

ok

#

so I do async?

#

and await?

#

const image = await cloudwatch.getMetricWidgetImage(WidgetDefinition, (err, data) => {

night wren
ashen surge
#
        if (err)
          console.log(err, err.stack); // an error occurred

        else {
          //image = new Buffer(data.MetricWidgetImage).toString('base64');
          const img = "stuff"
          return img.toString;

        }
      });

    then(image);```
ashen surge
#

I just need this return value outside to use it on another thing in the code

night wren
#

Yes, expect that you hope that const image will be something other than what the method returns, which is an AWS.Request. Don't have that expectation. Use the documentation to know what expectations you can have.

ashen surge
#

I know, that string "stuff" is an exemple

#

to test how do I get values to the outside of the function

#

and thats the problem

#

not the documentation and the aws function

#

you just derailing

night wren
#

Again, you can't do what you want. So, instead of doing

const image = cloudwatch.getMetricWidgetImage(WidgetDefinition,  (err, data) => { ... });
// use image here

Use

cloudwatch.getMetricWidgetImage(WidgetDefinition,  (err, data) => {   // use data here.
});
ashen surge
#

// use data here
and how do I get it out of the fucntion?

#

to an outside value

night wren
#

You don't. Because you can't. Not synchronously. You pass a callack, and it's inside the callback that you use it.

ashen surge
#

ok, then I go async

#

thats not a problem

ashen surge
#
        if (err)
          console.log(err, err.stack); // an error occurred
        else {
          return data.MetricWidgetImage.toString('base64');
        }
      });
image.then(data => {
      browser =  puppeteer.launch({});
      const page =  browser.newPage();
       page.setContent('<html><body><p>test 2 11 11'+data+'</p><img src="data:image/png;base64,'+data+'"/></body></html>', {
        waitUntil: "load",
      });
#

I make the first function a promise