0
I have the following code pad
server.get('/energy', (req, res, next) => {
{
const getScript = (url) => {
return new Promise((resolve, reject) => {
const http = require('http'),
https = require('https')
let client = http
if (url.toString().indexOf('https') === 0) {
client = https
}
client.get(url, (resp) => {
let data = ''
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk
})
// The whole response has been received. Print out the result.
resp.on('end', () => {
resolve(data)
})
}).on('error', (err) => {
reject(err)
})
})
};
(async (url) => {
console.log('Async ' + await getScript(url))
})('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3')
const ret = async (url) => {
const dados = await getScript(url)
console.log(url)
console.log('retorno: ' + dados)
return dados
}// ('http://minhaurl.com')
console.log('Dados de retorno: ' + ret('http://minhaurl.com'))
res.send(ret('http://minhaurl.com'))
next()
}
})
Where I’d like to return the value a certain page brings.
For example, the excerpt
(async (url) => {
console.log(await getScript(url))
})('http://minhaurl.com')
On the console shows like this:
Async [129.66,30.27,3924.31,2721.89,0.69,0.00,0.00,0.2826.94,0.00,129.56,31.45,4075.07,-2453.22,-0.60,0.00,0.00,3253.91,0.00,128.81,29.19,3760.38,-514.79,-0.14,-1.00,-1.00,-1.00,3724.98,59.76,30.60,35.10]
In this passage
const ret = async (url) => {
const dados = await getScript(url)
console.log(url)
console.log('retorno: ' + dados)
return dados
}
also comes back like this: ten-year-old:
[129.59,30.44,3944.05,2768.30,0.70,0.00,0.00,0.00,2809.28,0.00,129.60,31.92,4137.48,-2462.13,-0.60,0.00,0.00,3325.15,0.00,128.81,29.33,3778.27,-558.25,-0.15,-1.00,-1.00,-1.00,3736.80,59.86,30.60,35.00]
There I am trying to show this data on the page with this excerpt
console.log('Dados de retorno: ' + ret('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3'))
res.send(ret('http://localhost:81/energia/monitoramento/central.php?ip=147.1.3.3'))
next()
But the return says so:
Return data: [Object Promise]
And on the page it appears just like this: {}
How do I show the same data that appears in the console on the page?
Async [129.66,30.27,3924.31,2721.89,0.69,0.00,0.00,0.2826.94,0.00,129.56,31.45,4075.07,-2453.22,-0.60,0.00,0.00,3253.91,0.00,128.81,29.19,3760.38,-514.79,-0.14,-1.00,-1.00,-1.00,3724.98,59.76,30.60,35.10]
Try
console.log(await getScript(url).toString())
– Costamilam
He said he can’t use await out of an async function
– adventistaam
Then define it as asynchronous:
server.get('/energy', async (req, res, next) => {
– Costamilam
Keeps coming back:
Dados de retorno: [object Promise]
– adventistaam
change the variables of
const
forlet
, a tip: lambda expression is cool when you want to do a function of one or two lines, use large functionsfunction
, makes the code more readable (in my opinion)– Costamilam
I’m almost there, followed your management on the
await
but without the tostring at the end there returned, but came between the quotes ("
) and within the quotes at the end of the information came a\r\n
– adventistaam