后端使用axios的post请求music云函数,返回的是字符串,怎么转换成数组

来源:9-5 HTTP API触发云函数获取歌单列表

京2747767

2021-09-26

const Router = require(‘koa-router’)
const router = new Router()
const axios = require(‘axios’)
const getAccessToken = require(’…/utils/getAccessToken.js’)

router.get(’/list’, async (ctx, next) => {
//查询歌单列表
const ACCESS_TOKEN = await getAccessToken()
const ENV = 'test-6golf8e0c24e8273’
const url = https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=${ACCESS_TOKEN}&env=${ENV}&name=music
const data = await axios({
url,
method: ‘post’,
data: {
$url: ‘playlist’,
start: 0,
count: 50
}
}).then((res) => {
console.log(res.data)
return res.data
}).catch((error) => {
console.log(error)
})
ctx.body = {
data,
code:20000
}

})

module.exports = router

图片描述

写回答

1回答

谢成

2021-09-27

JSON.parse()方法可以把字符串转化为对象,然后就可以去掉对象下面的属性了。
0
2
京2747767
return res.data 能取到字符串歌单 return res,报错,提示这个信息 服务开启在3000端口 object TypeError: Converting circular structure to JSON --> starting at object with constructor 'ClientReques | property 'socket' -> object with constructor 'T --- property '_httpMessage' closes the circle at JSON.stringify () at respond (C:\Users\Administrator\Desktop\music-amin at handleResponse (C:\Users\Administrator\Desktop\mus at processTicksAndRejections (internal/process/task_q
2021-09-27
共2条回复

微信小程序云开发-从0打造云音乐全栈小程序

横跨小程序端、云后端、CMS一站式云开发的小程序全栈课程

1938 学习 · 2768 问题

查看课程