В настоящее время я работаю над проектом в javascript, где я должен получить билеты для их отображения. Итак, в основном здесь я получаю URL-адрес, за которым следует ключ API, ответ JSON, а затем хочу поместить результат в глобальную переменную. Вот код
var colors = { 10: "#33cc33", 20: "#ffcc00", 30: "#ff0066", 40: "##ff0000" }
var posts = [];
//query the ticket API
fetch(process.env.ticket_url.concat(apikey))
//transform the response into json datas
.then(res => res.json())
.then(res => {
//if the Key is wrong
if (res.status == 401) {
message.channel.send("Your API key is not valable, please set a valable one with !setinfo command followed by your key");
}
//if the Key is valid
else {
res.forEach(element => {
var post = {};
post[id] = element[id];
post[color] = colors[priority_id];
post[title] = element[title];
post[description] = element[description];
console.log(post)
posts.push(post);
})
}
// code to handle the error
}).catch(err => {});
Однако, как вы можете видеть, я ждал, чтобы получить столько же сообщений, сколько и мой API, благодаря console.log(post).... Но я ничего не получил. Я хотел добавить, что
var colors = { 10: "#33cc33", 20: "#ffcc00", 30: "#ff0066", 40: "##ff0000" }
var posts = [];
//query the ticket API
fetch(process.env.ticket_url.concat(apikey))
//transform the response into json datas
.then(res => res.json())
.then(res => {
//if the Key is wrong
if (res.status == 401) {
message.channel.send("Your API key is not valable, please set a valable one with !setinfo command followed by your key");
}
//if the Key is valid
else {
console.log(res)
}
// code to handle the error
}).catch(err => { });
Возвращает ожидаемый ответ в этом формате:
[ .
.
.,
{
id: 222610,
title: '456',
description: '== what is the issue? ==\n\n== enter steps to reproduce ==',
user_id: 808,
company_id: 76,
assigned_to_id: 805,
status_id: 60,
priority_id: 30,
ticket_queue_id: 69,
rating: null,
rated_on: null,
created_on: '2020-08-14T18:24:37.000Z',
updated_on: '2020-08-14T18:24:54.000Z',
status_changed_on: '2020-08-14T18:24:54.000Z',
solved_on: '2020-08-14T18:24:54.000Z',
assigned_on: '2020-08-14T18:24:39.000Z',
created_from: 0,
ticket_type_id: null,
cc: '',
legacy_id: null,
first_assigned_on: '2020-08-14T18:24:39.000Z',
user_attention_id: null,
due_on: '2020-08-29T11:59:59.000Z',
scheduled_on: '2020-08-28T11:59:59.000Z',
is_attention_required: true,
ticket_form_id: 425,
resolution_id: 1
}
]
Итак, вопрос: я не знаю, что не так в этом коде, можете ли вы помочь мне понять это, потому что я ищу время, но не могу понять это сам. Спасибо за вашу помощь.