尝试使用 node-fetch 启动机器人时出错

Posted

技术标签:

【中文标题】尝试使用 node-fetch 启动机器人时出错【英文标题】:Error when tryign to start the bot with node-fetch 【发布时间】:2020-01-10 05:20:08 【问题描述】:

我目前正在根据之前的代码设置一个高级队列机器人,但我需要从 snekfetch 切换到 node fetch

我已经尝试使用get函数但无济于事,结果显示不存在

run(msg,  user ) 
            fetch.get('https://api.2b2t.dev/prioq').then(r => 
            fetch.get('https://2b2t.io/api/queue').then(qwerty => 

                let entry = r.body[1]
                let response = qwerty.body[0][1]

                  client.user.setActivity("Queue: " + response + " PrioQueue: " + entry);
                if(message.channel.id === 598643633398349854) 
                  message.delete().catch(O_o=>); 
                  message.author.send("The owner has disabled this command in this channel!")
                  return
                
                const queueembed = new RichEmbed()
                .setColor("#32CD32")
                 .addField('Regular Queue:', response, true)
                 .addField('Priority Queue:', entry, true)
                 .addField('Regular Queue Time:', Math.round(response * 0.7) + " minutes.", true)
                 .addField('Priority Queue Time:', Math.round(entry * 0.7) + " minutes.", true)
                 .setThumbnail('https://i.redd.it/jyvrickfyuly.jpg')
                 .setFooter("https://discordapp.com/invite/uGfHNVQ")
                 message.channel.send(queueembed).then(msg => 
        var timerID = setInterval(function() 
            const queueembed = new RichEmbed()
            .setColor("#32CD32")
            .addField('Regular Queue:', response, true)
            .addField('Priority Queue:', entry, true)
            .addField('Regular Queue Time:', Math.round(response * 0.7) + " minutes.", true)
            .addField('Priority Queue Time:', Math.round(entry * 0.7) + " minutes.", true)
            .setThumbnail('https://i.redd.it/jyvrickfyuly.jpg')
            .setFooter("https://discordapp.com/invite/uGfHNVQ")
            message.channel.edit(msg)
        , 5 * 1000); 
    ))
    )
  

通常机器人会启动,但会弹出此错误

我尝试在 fetch 和删除 get 之间切换,但我很困惑 下一步该做什么

"TypeError: Cannot read property 'get' of undefined"

【问题讨论】:

【参考方案1】:

貌似有两个问题:

1) fetch 未定义(您需要安装/需要它吗?)

2) get 不是 fetch API 的一部分

对于 GET 请求,您只需执行 fetch('<url>')

将两者放在一起:

const fetch = require('node-fetch')

fetch('https://api.2b2t.dev/prioq').then(r => 
  fetch('https://2b2t.io/api/queue').then(qwerty => 
    // ...rest
  )
)

https://github.com/bitinn/node-fetch#api

编辑 您还需要使其余代码符合获取要求。根据 fetch 规范,响应公开的 bodyReadableStream,这可能会导致您的错误。从它的界面可以看出,它还公开了text()json() 方法:

interface mixin Body 
  readonly attribute ReadableStream? body;
  readonly attribute boolean bodyUsed;
  [NewObject] Promise<ArrayBuffer> arrayBuffer();
  [NewObject] Promise<Blob> blob();
  [NewObject] Promise<FormData> formData();
  [NewObject] Promise<any> json();
  [NewObject] Promise<USVString> text();
;

https://fetch.spec.whatwg.org/#body-mixin

我认为响应是 JSON,所以您需要使用 response.json()

fetch('https://api.2b2t.dev/prioq').then(r => r.json()).then(r => 
  fetch('https://2b2t.io/api/queue').then(qwerty => qwerty.json()).then(qwerty => 
    // ...rest
  )

【讨论】:

好的,我已经尝试过了,但现在我收到错误“无法读取未定义的 '1' 的属性”

以上是关于尝试使用 node-fetch 启动机器人时出错的主要内容,如果未能解决你的问题,请参考以下文章

我在编写不和谐机器人时出错

Xcode OSX 服务器“创建机器人时出错”

在列表视图中填充电话联系人时出错

使用 Xamarin.Mobile 读取 Xamarin 表单中的联系人时出错

在使用 node-fetch 解析为 JSON 之前替换原始数据中的字符

从光标检索联系人时出错