A
A
Alexander Ivanov2018-02-11 17:46:05
JavaScript
Alexander Ivanov, 2018-02-11 17:46:05

How to change the type of undefined?

From the fact that I will scold JS for what it is clear, and praise it in the same way, nothing will change.
Therefore, I will ask. Who also copes with errors like undefined?
In my case, I want that if the loop stumbles upon undefined, then it does not stop, but goes on.

axios.jsonp('https://api.vk.com/api.php?oauth=1&method=wall.get&domain='+this.state.userInput+'&count='+this.state.count+'&access_token='+this.state.accessToken+'&fields='+this.state.userGroup)
                .then((data) =>{
                    //generate needed arrays
                    let result_arr = [],
                        i = 0;
                    while (i < 50) {
                        i++;

                        // let { photo: {src,src_big} } = data.response[i++].attachment.photo;
                        let { attachment, likes, comments } = data.response[i++];

                        attachment = attachment[attachment.type];
                        if(typeof attachment === undefined){
                            console.log('sdsds');
                        }

                        result_arr[result_arr.length] = {attachment,likes,comments};
                        // console.log(result_arr);
                        console.log(i++);
                    }
                    console.log(result_arr);
                    console.log(data.response);
                    this.setState({
                        // id_articles: data.response[index].id,
                        articles: result_arr
                    });
                });

Why is it necessary - vk has a terribly crooked API, some differences in the names of pictures are worth it (here at least NULL is not scary), but the wrong type leads to undefined everything breaks off
for example here:
{
"id": 393,
"from_id": 7832775,
"to_id": 7832775,
"date": 1494069770,
"post_type": "copy",
"text": "Спасатели Малибу. Правдивая версия",
"copy_post_date": 1494053105,
"copy_post_type": "post",
"copy_owner_id": -42510378,
"copy_post_id": 170500,
"attachment": {
"type": "doc",
"doc": {
"did": 445046357,
"owner_id": 86240998,
"title": "vk.com/gif_fak",
"size": 5003294,
"ext": "gif",
"url": "httpdf&dl=GA:1518359452:f39677c961b5686268&api=1&no_preview=1",
"date": 1494044240,
"thumb": "httpdfh-3/m_f5889bbd90.jpg",
"thumb_s": "https://d0363/d7/-3/s_f5889bbd90.jpg",f
"access_key": "3e7f03e5983003a019"
}
},
"attachments": [
{
"type": "doc",
"doc": {
"did": 445046357,
"owner_id": 86240998,
"title": "vk.com/gif_fak",
"size": 5003294,
"ext": "gif",
"url": "https://vk.cd90e760488c06c&dl=GA:1518359452:f39677c961b5686268&api=1&no_preview=1",
"date": 1494044240,
"thumb": "https://sun9sd9bbd90.jpg",
"thumb_s": "https://sunsd889bbd90.jpg",
"access_key": "3e7f03e5983003a019"
}
}
],
"comments": {
"count": 0
},
"likes": {
"count": 1
},
"reposts": {
"count": 0
}
},

Instead of type: photo type = doc etc. Although essentially the same picture, in general, the contact api is a complete disappointment.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question