Node Result js restler "get" did not complete when trying to return result

I am trying to get an HTML site using restler. But when I try to get the corresponding part of the result, I always get an error,

"TypeError: Unable to read the" rawEncoded "property from undefined".

'rawEncoded' sometimes it's 'res'. I think this varies with processing time.

I am trying to get result.request.res.rawEncode from a restler get result.

My function:

var rest = require('restler'); var loadHtmlUrl = function(weburl) { var resultstr = rest.get(weburl).on('complete', function(result) { var string = result.request.res.rawEncode; return string; }); return resultstr; }; 

Then:

 var htmlstring = loadHtmlUrl('http://google.com'); 

Maybe a restler is a completely wrong path. Perhaps I do not fully understand this. But I'm definitely stuck ...

Thanks!

0
source share
1 answer

Will your return resultstr; launched before the on('complete' callback is called because it is asynchronous, so your htmlstring will be empty as a result? I think you need to have the callback as a parameter for your loadHtmlUrl , for example:

 var rest = require('restler'); var loadHtmlUrl = function(weburl, callback) { var resultstr = rest.get(weburl).on('complete', function(result) { callback(result.request.res.rawEncode); }); }; 

And then call it like this:

 var htmlstring = null; loadHtmlUrl('http://google.com', function(rawEncode) { htmlstring = rawEncode; //Do your stuff here... }); 

I think this will solve the future problems that you will have. However, I think the real problem that you are facing is that result.request does not have a res property. I think my change above can solve this problem (not quite sure how). If not, then I would recommend looking at what properties result.request has as a debug starter ...

+3
source

Source: https://habr.com/ru/post/1490645/


All Articles