1

I've got this error using res.json to get the return of my function and this is code is been used em anothers places as wrote below, running fine.

async function getPlaylist(req, res, playlistId) {

  try {

    // const calltoken = await getToken()

    // const token = calltoken.data.access_token

    // console.log(token)

    const config = {
      headers: {
        'Authorization': 'Bearer ' + 'BQA0K9bKgBVn8xTp-yTsoaKs5VfS7EyjMIL03OEOy05wq08ZmLkNfqbbnsL_hFT1AV2FGN5tAQdeDV1X224', //token,
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    }

    const url = 'https://api.spotify.com/v1/playlists/1j2L3DjzZ3SdN64r83Sblj?si=cuvOrPONSO6caE9XD6smEg'

    await axios.get(url, config)
      .then(function (response) {

        var playlist = response

        var items = playlist.data.tracks.items

        // console.log(items)

        const playlistfull = []

        items.forEach(index => {

          var playlistdata = {
            name: index.track.name,
            artists: index.track.album.artists[0].name,
            album: index.track.album.name,
            url: index.track.external_urls.spotify
          }

          playlistfull.push(playlistdata)

        })

        return res.json(playlistfull)

      })

  } catch (error) {

    return console.log(error)

  }
}
2
  • 2
    Why should res.json be a function? What is res? It's the second argument to getPlaylist but you haven't show us how that function is called so we can't know what the value actually is. Since it doesn't have a json method but you think it should be, that value probably isn't what you think it is. Commented Dec 23, 2019 at 2:22
  • Does this answer your question? TypeError: res.json is not a function Commented Dec 23, 2019 at 2:55

1 Answer 1

1

You have to use in NodeJS
Node's res give parameter to function

const router = express.Router();
router
    .route('/')
    .get((req, res) => {
        const playlistId = 'asdf';
        getPlaylist(req, res, playlistId);
        return;
    });

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.