【问题标题】:Foreach fetch response javascriptForeach 获取响应 javascript
【发布时间】:2020-09-22 19:43:09
【问题描述】:

我目前正在使用 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 => { });

正在以这种格式返回 excpeted 响应:

[ .
  .
  .,
  {
    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
  }
]

所以问题是:我不知道这段代码有什么问题,你能帮我弄清楚吗,因为我正在寻找时间,但我自己无法弄清楚。 感谢您的帮助。

【问题讨论】:

    标签: javascript api fetch


    【解决方案1】:

    您需要将键 - id、title ... 用引号括起来。即 post["id"] = element["id"] 等等。

    【讨论】:

    • 这解决了我的问题。我真的认为这是一个获取问题,但实际上我很愚蠢。谢谢队友:D
    猜你喜欢
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2021-03-23
    • 2020-08-11
    • 2014-03-05
    • 2014-02-27
    相关资源
    最近更新 更多