我试图做一个相当复杂(至少对我而言)的API请求,涉及获取一系列报告。报表对象本身具有嵌套对象,因此我试图同时提取相关数据。报告对象如下所示:
reports: { loading: false, error: null, data: [{id:'', date:'',
student_id:'',
feeding:[],diapering:[], nap:[], meds:[], playTime:[], comments:[],
supplies:[]}] }
我已经找到了如何使用多个相关表格获取一份报告,但我还没有找到获取报告数组的语法,每种报告都有自己的相关数据。
这就是我现在所拥有的:
exports.getReport = (date) => {
console.log(id)
return Reports.where(date)
.fetchAll({
withRelated: ['feeding', 'comment', 'diapering', 'nap', 'meds', 'playTime', 'supplies']
})
.then(report => {
const meds = report.related('meds')
const nap = report.related('nap')
const feeding = report.related('feeding')
const diapering = report.related('diapering')
const supplies = report.related('supplies')
const playTime = report.related('playTime')
const comm = report.related('comment')
const medsList = meds.map(med => {
return med.attributes
})
const napList = nap.map(n => {
return n.attributes
})
const feedingList = feeding.map(feed => {
return feed.attributes
})
console.log(feedingList)
const diaperList = diapering.map(diaper => {
return diaper.attributes
})
const suppliesList = supplies.map(supply => {
return supply.attributes
})
const playTimeList = playTime.map(play => {
return play.attributes
})
const commentList = comm.map(com => {
return com.attributes
})
return reports
const reports = report.models.map(rep => {
return rep.attributes
})
return [feedingList, commentList, napList, playTimeList,
suppliesList, diaperList, medsList, reports]
})
.catch(err => {
console.log(err)
})
}
此语法适用于
.fetch()
,但没有
使用fetchall()
我得到了
report.related不是函数错误