#I'm a bit lost How to I get the return value to const image?
46 messages · Page 1 of 1 (latest)
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
@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).
I'm assigning to const image the return ?
return img should go to const image
or anything else I return right?
@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
```
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
No, it shouldn't.
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.
how can I get a value to the outside of the function?
You're assigning what getMetricWidgetImage returns, and we don't know what it is unless you point us to the documentation of that function.
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?
You can use the GetMetricWidgetImage API to retrieve a snapshot graph of one or more Amazon CloudWatch metrics as a bitmap image. You can then embed this image into your services and products, such as wiki pages, reports, and documents. You could also retrieve images regularly, such as every minute, and create your own custom live dashboard.
ok
so I do async?
and await?
const image = await cloudwatch.getMetricWidgetImage(WidgetDefinition, (err, data) => {
No. You read the documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatch.html#getMetricWidgetImage-property. It explains how to use the function, with examples and everything.
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);```
and I'm already using like the exemples
I just need this return value outside to use it on another thing in the code
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.
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
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.
});
You don't. Because you can't. Not synchronously. You pass a callack, and it's inside the callback that you use it.
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