【问题标题】:Cannot read property 'map' of undefined problem无法读取未定义问题的属性“地图”
【发布时间】:2021-03-24 18:17:39
【问题描述】:

我有这个错误

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined 在 C:\Users\Misha\Desktop\slaves\test2.js:23:18

在 processTicksAndRejections (internal/process/task_queues.js:93:5)

在异步 Timeout.buyFetter [as _onTimeout] (C:\Users\Misha\Desktop\slaves\test2.js:16:5)

你能帮帮我吗? 这是我的代码:

    async function buyFetter() {
    await fetch("https://pixel.w84.vkforms.ru/HappySanta/slaves/1.0.0/start", {
        method: "GET",
        headers: {
            "Content-Type": "application/json",
            authorization: config.authorization
        }
    }).then(a => a.json()).then(a => {
        a.slaves.map(async b => {
            if (0 === b.fetter_to) await axios.post("https://pixel.w84.vkforms.ru/HappySanta/slaves/1.0.0/buyFetter", {
                slave_id: b.id
            }, {
                headers: {
                    authorization: config.authorization
                }
            }), vk.api.messages.send({
                peer_id: config.vk.chat,
                random_id: 0,
                message: `Купил оковы для @id$ {
                    b.id
                }`
            });
            else if ("" === b.job.name) {
                let c = utils.pick(["утка", "@ut1ka"]);
                await axios.post("https://pixel.w84.vkforms.ru/HappySanta/slaves/1.0.0/jobSlave", {
                    slave_id: b.id,
                    name: c
                }, {
                    headers: {
                        authorization: config.authorization
                    }
                }), vk.api.messages.send({
                    peer_id: config.vk.chat,
                    random_id: 0,
                    message: `Установил работу для @id$ {
                        b.id
                    } | Работа: $ {
                        c
                    }`
                })
            }
        })
    })
}

【问题讨论】:

  • 你可以打印 a.json() 看看它是什么结构

标签: javascript


【解决方案1】:
You may need to have look of async/await usage.

async function buyFetter() {
  try {
    const res = await fetch("https://pixel.w84.vkforms.ru/HappySanta/slaves/1.0.0/start", {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        authorization: config.authorization
      }
    });
    const data = await res.json();
    data.slaves.map(async b => {
       console.log(b);
    })
  } catch (err) {
    console.log(err);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多