E
E
egorlisss2019-04-16 11:37:54
JavaScript
egorlisss, 2019-04-16 11:37:54

How to return a response on error?

vk.updates.hear(/^(?:hi)\s([^]+)?/i, (message) => { 
    axios.get(`https://minecraft-statistic.net/api/player/info/${message.$match[1]}/200940`).then(res => {
  if((res.data && res.data.status) == 'ok'){
  if(res.data && res.data.data) {
    let timeplay = res.data.data.total_time_play || 0
  var num = timeplay;
    var hours = (num / 60);
    var play = Math.floor(hours);
  if(res.data && res.data.data) {
  let visit = res.data.data.visited_server
  axios.get(`https://api.ashcon.app/mojang/v1/user/${message.$match[1]}`).then(resp => {
    return resp.data.username_history;
    }).then(data => {
    let names = ''; 
        data.forEach(item => {
    names += item.username + '\n';
  })
  return message.send(`История никнейма:\n\n${names}`)
  }).catch(err => message.send(`${err}`));
  }}}}else message.send(`❗ Никнейм ${message.$match[1]} не существует!`)
}).catch(err => message.send(`⚠ Произошла ошибка!`));
});

If ${resp} returns Error: Request failed with status code 404 , then else is executed, but I need return message.send to be executed, but if the response comes with an error, then replace this variable with 0

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
egorlisss, 2019-04-16
@egorlisss

return message.send(`‍♂ ${message.$match[1]}\n\n ${visit > false ? 'Есть доступ к СП-Мини 💚' : 'Не покупал доступ на СП-Мини 💔'}\nВ последний раз играл: ${lastplay}\nВсего наиграно: ${play} часов\n\nИстория никнейма:\n\n${names}`)
  }).catch(err => message.send(`‍♂ ${message.$match[1]}\n\n ${visit > false ? 'Есть доступ к СП-Мини 💚' : 'Не покупал доступ на СП-Мини 💔'}\nВ последний раз играл: ${lastplay}\nВсего наиграно: ${play} часов`));
  }}}}else message.send(`❗ Никнейм ${message.$match[1]} не существует!`)

B
bqio, 2019-04-16
@bqio

Either throw in code from resources like jsfiddle.net , or properly indent. And also it is worth deciding whether you are using var, or let.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question