简体   繁体   English

res.json 不是函数 - nodejs

[英]res.json is not a function - nodejs

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.我使用 res.json 得到这个错误来获取我的函数的返回值,这是代码在其他地方使用,如下所述,运行良好。

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)

  }
}

You have to use in NodeJS你必须在 NodeJS 中使用
Node's res give parameter to function节点的resfunction提供parameter

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM