Https request timeout using js

Hi
since i failed to make the https request using codeless im trying js now.
here is my code:

class MyService {
  /**
   * @param {String} name
   */ 
  helloWorld( name ) {

    const https = require('https');
  
  return new Promise((resolve, reject) => {
    https.get('https://jsonplaceholder.typicode.com/todos', (res) => {
      let body = '';
  
      res.on('data', (chunk) => body += chunk);
      res.on('end', () => {
        return "yes";
  
        resolve();
      });
  
      res.resume();
    }).on('error', reject);
  });
  
  }
}

Backendless.ServerCode.addService( MyService );

and i get this error:

Task execution is aborted due to timeout

Hi! Have you started JSCodeRunnner? I advise you to check this guide:
https://backendless.com/docs/bl-js/#bl_basic_quick_start_guide_js

thanks you for your replay

there is no errors on the log if that is what you mean.

the request should take less than one second to finish. and you can test it. its a json test service.

Hi @mohammad_altoiher

try this one:

  helloWorld () {
    return Backendless.Request.get('https://jsonplaceholder.typicode.com/todos')
  } 

and also you can read more about Backendless.Request here https://github.com/Backendless/Request

Regards, Vlad

1 Like

Thanks @vladimir-upirov
one more question:
how can i parse the result properly?

for example if this is the result of the request :

{
    "error_message": null,
    "subresource_uris": {
        "media": "/2010-04-01/Acco/Media.json"
    }

if i want to only return “error_message”
in normal js i should say result.error_message

but that didn’t work.

Please rephrase your question so it is written in Backendless terms. If it has nothing to do with Backendless, consider posting it to a support forum of the related technology.

Regards,
Mark

Hey @mark-piller
Its about Backendless request (the link by @vladimir-upirov)
There is no documentation on the results, so im a bit lost

And normal js code didn’t provide the desired results also.

Backendless.Request.get returns Promise

did you read this doc https://github.com/Backendless/Request ?

try this one:

helloWorld () {
    return Backendless.Request.get('https://jsonplaceholder.typicode.com/todos')
           .then(result => {
                 console.log('result', result)
                 console.log('error_message', result.error_message)
                 console.log('subresource_uris', result.subresource_uris)
           })
  }  

but actually this https://jsonplaceholder.typicode.com/todos end point returns an array

Regards, Vlad

1 Like

also you can use “async/await”

async helloWorld () {
    const result = await Backendless.Request.get('https://jsonplaceholder.typicode.com/todos')
        
     console.log('result', result)
     console.log('error_message', result.error_message)
     console.log('subresource_uris', result.subresource_uris)

    return result.error_message
  }
1 Like