0

I have this function

function getJsonObjectFromURL(url, onData) {
  let chunks = [];
  return require('https').get(url, res => {
    res.setEncoding('utf8')
      .on('data', (chunk) => {
        chunks.push(chunk);
      })
      .on('end', () => {
        onData(JSON.parse(chunks.join('')));
      });
  }).on('error', function(e) {
    console.log("Got an error: ", e);
  });
}

Also I have this script that converts url's data to json array.

url = https://pu.vk.com/c824502/upload.php?act=do_add&mid=213468131&aid=-14&gid=156603484&hash=7ab9a7e723425f4a6ca08709cbd5ebd0&rhash=ba8f0ec6580a6eafce38349b12ed3789&swfupload=1&api=1&wallphoto=1
    getJsonObjectFromURL(url, data => {
      console.log(data.server, data.photo, data.hash);
    });

It goes well when console.log. But when I want to make from this script variable, it gives me huge collection

var xx = getJsonObjectFromURL(url, data => {
  return data.server;
});
console.log(xx);

enter image description here

1 Answer 1

1

Your function getJsonObjectFromURL() doesn't return the object returned by the URL. It returns the object responsible for the https request code, which is something you don't want.

I see that you are using ES6, so the best solution for you is to probably create an async function that returns a promise, which will give you great flexibility. Here is an improved version of your code:

const https = require('https');

async function getJsonObjectFromURL(url) {
    return new Promise((resolve, reject) => {
        const chunks = [];
        try {
            https.get(url, res => {
                res.setEncoding('utf8')
                .on('data', (chunk) => {
                    chunks.push(chunk);
                })
                .on('end', () => {
                    resolve(JSON.parse(chunks.join('')));
                });
            }).on('error', e => reject(e));
        } catch (err) {
            reject(err);
        }
    });
};

This code allows you to retrieve the remote contents of the HTTPS url synchronously or asynchronously.

Asynchronous Call

As you have already done in your code, you can use a lambda callback that handles the response when it is ready.

const url = 'https://pu.vk.com/c824502/upload.php?act=do_add&mid=213468131&aid=-14&gid=156603484&hash=7ab9a7e723425f4a6ca08709cbd5ebd0&rhash=ba8f0ec6580a6eafce38349b12ed3789&swfupload=1&api=1&wallphoto=1';

// here we use a lambda callback that handles the response
getJsonObjectFromURL(url)
    .then(data => {
        console.log(data.server, data.photo, data.hash);
    })
    .catch(err => console.error(err));

Synchronous Call

The synchronous call forces the function to wait for the result. This is how you can do it:

async function getSync() {
    try {
        // wait for the result
        const data = await getJsonObjectFromURL(url);
        console.log(data.server);
    } catch(err) {
        console.error(err);
    } 
}   
getSync();  

Please note that we can only use the await keyword when we are inside an async function. This is why I had to wrap the synchronous call with a function.

Sign up to request clarification or add additional context in comments.

2 Comments

But there is console.log, but I wanted to get result to variable to use in other function. How can I do then?
In the Synchronous Call example, the "data" variable receives the result. You can pass that variable to other functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.