Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript youtube搜索api函数外部未定义变量_Javascript_Node.js_Youtube_Youtube Api_Async Await - Fatal编程技术网

Javascript youtube搜索api函数外部未定义变量

Javascript youtube搜索api函数外部未定义变量,javascript,node.js,youtube,youtube-api,async-await,Javascript,Node.js,Youtube,Youtube Api,Async Await,我正在从youtube api获取播放ID 当控制台输出在youtube搜索功能中时,它会给出正确的输出 var playlistId; async function suggestTrack(genre) { youtube.search.list({ auth: config.youtube.key, part: 'id,snippet', q: genre }, function (err, data) { if (err

我正在从youtube api获取播放ID

当控制台输出在youtube搜索功能中时,它会给出正确的输出

 var playlistId;
async function suggestTrack(genre) {

    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, function (err, data) {
      if (err) {
        console.error('Error: ' + err);
      }
      if (data) {
        console.log(data.items[0].id.playlistId); //getting the id
        playlistId = data.items[0].id.playlistId;

      }
      //process.exit();
    });

    console.log(playlistId);// undefined

const tracks = await youtube_api.getPlaylistTracks(playlistId);
return tracks[Math.floor(tracks.length * Math.random())];

}
它提供了未定义的外部youtube搜索api函数

 var playlistId;
async function suggestTrack(genre) {

    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, function (err, data) {
      if (err) {
        console.error('Error: ' + err);
      }
      if (data) {
        console.log(data.items[0].id.playlistId); //getting the id
        playlistId = data.items[0].id.playlistId;

      }
      //process.exit();
    });

    console.log(playlistId);// undefined

const tracks = await youtube_api.getPlaylistTracks(playlistId);
return tracks[Math.floor(tracks.length * Math.random())];

}

API调用是异步的。在api响应返回之前,您正在打印播放ID的值。你必须等待回应。由于您正在使用
async
将api调用包装在
Promise
中,并使用
wait
。要实现
search.list
方法,您有很多选择,也可以自己做,如下所示

function search(key, part, genre) {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: key,
      part: part,
      q: genre
    }, function (err, data) {
      if (err) {
        reject(err);
        return;
      }
      // use better check for playlistId here
      resolve(data ? data.items[0].id.playlistId : null);
    })
  });
}

// then use it here
async function suggestTrack(genre) {
  const playlistId = await search(config.youtube.key, 'id,snippet', genre);      
  const tracks = await youtube_api.getPlaylistTracks(playlistId);
  return tracks[Math.floor(tracks.length * Math.random())];
}

API调用是异步的。在api响应返回之前,您正在打印播放ID的值。你必须等待回应。由于您正在使用
async
将api调用包装在
Promise
中,并使用
wait
。要实现
search.list
方法,您有很多选择,也可以自己做,如下所示

function search(key, part, genre) {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: key,
      part: part,
      q: genre
    }, function (err, data) {
      if (err) {
        reject(err);
        return;
      }
      // use better check for playlistId here
      resolve(data ? data.items[0].id.playlistId : null);
    })
  });
}

// then use it here
async function suggestTrack(genre) {
  const playlistId = await search(config.youtube.key, 'id,snippet', genre);      
  const tracks = await youtube_api.getPlaylistTracks(playlistId);
  return tracks[Math.floor(tracks.length * Math.random())];
}

youtube.search.list
是异步的。您正在尝试访问播放ID,因为它是同步进程的一部分

您可以将
youtube.search.list
包装在
Promise
中,以简化其使用


老路

function wrappedSearch() {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, (err, data) => {
      if (err) {
        console.error('Error: ' + err);

        return reject(err);
      }

      return resolve((data && data.items[0].id.playlistId) || false);
    });
  });
}

async function suggestTrack(genre) {
  const playlistId = await wrappedSearch();

  // Here playlistId is either the playlistId, either false

  console.log(playlistId);

  const tracks = await youtube_api.getPlaylistTracks(playlistId);

  return tracks[Math.floor(tracks.length * Math.random())];
}

新方式

function wrappedSearch() {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, (err, data) => {
      if (err) {
        console.error('Error: ' + err);

        return reject(err);
      }

      return resolve((data && data.items[0].id.playlistId) || false);
    });
  });
}

async function suggestTrack(genre) {
  const playlistId = await wrappedSearch();

  // Here playlistId is either the playlistId, either false

  console.log(playlistId);

  const tracks = await youtube_api.getPlaylistTracks(playlistId);

  return tracks[Math.floor(tracks.length * Math.random())];
}
在节点v8中可用


youtube.search.list
是异步的。您正在尝试访问播放ID,因为它是同步进程的一部分

您可以将
youtube.search.list
包装在
Promise
中,以简化其使用


老路

function wrappedSearch() {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, (err, data) => {
      if (err) {
        console.error('Error: ' + err);

        return reject(err);
      }

      return resolve((data && data.items[0].id.playlistId) || false);
    });
  });
}

async function suggestTrack(genre) {
  const playlistId = await wrappedSearch();

  // Here playlistId is either the playlistId, either false

  console.log(playlistId);

  const tracks = await youtube_api.getPlaylistTracks(playlistId);

  return tracks[Math.floor(tracks.length * Math.random())];
}

新方式

function wrappedSearch() {
  return new Promise((resolve, reject) => {
    youtube.search.list({
      auth: config.youtube.key,
      part: 'id,snippet',
      q: genre
    }, (err, data) => {
      if (err) {
        console.error('Error: ' + err);

        return reject(err);
      }

      return resolve((data && data.items[0].id.playlistId) || false);
    });
  });
}

async function suggestTrack(genre) {
  const playlistId = await wrappedSearch();

  // Here playlistId is either the playlistId, either false

  console.log(playlistId);

  const tracks = await youtube_api.getPlaylistTracks(playlistId);

  return tracks[Math.floor(tracks.length * Math.random())];
}
在节点v8中可用


@YaShChaudhary我在命名变量时犯了一个错误。我刚刚更改了it@YaShChaudhary我在命名变量时犯了一个错误。我刚刚更改了它