zyrouge / node-genius-lyrics

Simple lyrics fetcher that uses Genius. Also has official API implementations.
https://genius-lyrics.js.org
MIT License
61 stars 12 forks source link

How can I get the lyrics of a song that I have acquired with ID? #12

Closed Gullinkambi-Whip closed 3 years ago

Gullinkambi-Whip commented 3 years ago

I apologize for this very rudimentary question.
Could you please tell me how to get the lyrics of a single song from the ID?


I am trying to display a list of songs from the keywords entered by the user, and then display the lyrics of the song the user selects from the list.
The API is as follows.
getSongsAPI

import Genius from "genius-lyrics";
const Client = new Genius.Client("API key");

const getSongs = async ({ query: { keyword } }, res) => {
  const songs = await Client.songs.search(keyword);
  return res.status(200).json(songs);
};

I was able to get a list of lyrics with the above API.
For each of the list of returned songs, we have planted a click event as shown below.

const getLyric = async ({currentTarget: { id } => {
  const res = await fetch("/api/getLyric/?id=" + id);
  const lyric = await res.json();
  console.log(lyric);
};

...
<li onClick={getLyric} id={song.id}>{song.title}</li>

getLyricAPI

import Genius from "genius-lyrics";
const Client = new Genius.Client("API key");

const getLyric = async ({ query: { id } }, res) => {
  const song = await Client.songs.get(Number(id));
  const lyric = await song.lyrics();
  return res.status(200).json(lyric);
};

The getLyricAPI expects to return lyrics, but it returns an array as shown in the image below.
res

zyrouge commented 3 years ago

Can you try logging const song = await Client.songs.get(Number(id)); and const lyric = await song.lyrics();? Seems like its working for me right now.

Gullinkambi-Whip commented 3 years ago

I was able to get the lyrics by rewriting the code as follows. It doesn't seem to be a bug.

-  return res.status(200).json(lyric);
+  return res.status(200).json({lyric: lyric});
zyrouge commented 3 years ago

Aight, closing this. Happy Coding!