NodeJs - youtube.search.list返回id:[object]

时间:2018-01-12 21:10:31

标签: node.js google-api youtube-data-api discord.js

const commando = require('discord.js-commando');
const config = require(__dirname + "/../../config/settings.json");
const google = require('googleapis');
const ytdl = require('ytdl-core');
const youtube = google.youtube({
 version: 'v3',
 auth: config.gapi
});
class PlayCommand extends commando.Command {
constructor(client) {
super(client, {
  name: 'play',
  group: "music",
  memberName: 'play',
  description: 'Plays music from youtube',
});
}
async run(message, args) {
youtube.search.list({
  maxResults: '5',
  part: 'snippet',
  type: 'video',
  q: args
},
function (err, request) {
  if (err) {
    console.error('Error: ' + err);
  }
if (request) {
  console.log(request)
}
});
}
}
module.exports = PlayCommand

到目前为止,这是我的播放命令,问题是每当我输入>play args时, 我明白了:

{ kind: 'youtube#searchListResponse',
 etag: '"S8kisgyDEblalhHF9ooXPiFFrkc/C1WPrLHrbjq3iYDhZhQ4QT0ahKc"',
 nextPageToken: 'CAUQAA',
 regionCode: 'PL',
 pageInfo: { totalResults: 1000000, resultsPerPage: 5 },
items:
  { kind: 'youtube#searchResult',
   etag: '"S8kisgyDEblalhHF9ooXPiFFrkc/HI2NmsFJ4wN7_PF5bDbDq-1kshA"',
   id: [Object],
   snippet: [Object] },

目前只有视频的ID很重要,但正如您所看到的那样,它返回[Object] 我真的不知道该怎么做,任何帮助都会很棒...... 谢谢你的到来!

1 个答案:

答案 0 :(得分:0)

记录YouTube搜索结果时,通常不会显示任何深层对象。因此,对象确实存在,您只需要将其记录下来。例如,

console.log(request.items[0]);

这应该输出整个第一个结果,包括找到的对象。另一种快速获得结果的好方法是YouTube-Search模块。根据我使用Google API的经验,它有时会非常奇怪并且会不断变化,因此使用模块可以帮助您跟上Google的变化。

相关问题