歌单数据无法去重
来源:3-10 优化接口请求方式及数据批量插入
幕布斯3491790
2021-04-14
云函数运行一次,playlist数据多一些。
如果53行和54行跟老师的代码不一样,就无法去重。
如果跟老师写成一样,就会显示错误,错误信息是
{“errorCode”:1,“errorMessage”:“user code exception caught”,“stackTrace”:“TypeError: Cannot read property ‘id’ of undefined\n at Runtime.exports.main [as handler] (/var/user/index.js:54:43)\n at process._tickCallback (internal/process/next_tick.js:68:7)”,“statusCode”:430}
下面是老师的代码
//
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const URL = 'https://apis.imooc.com/personalized?icode=CDD8F0D419BEEF3B'
const axios = require('axios')
const playlistCollection = db.collection('playlist')
const MAX_LIMIT = 100
// 云函数入口函数
exports.main = async (event, context) => {
// const countResult = await playlistCollection.count()
// const list = await playlistCollection.get()
const countResult = await playlistCollection.count()
const total = countResult.total
const batchTimes = Math.ceil(total/MAX_LIMIT)
const tasks = []
for (let i = 0; i < batchTimes; i++) {
let promise = playlistCollection.skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
tasks.push(promise)
}
let list = {
data: []
}
if (tasks.length > 0) {
list = (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data)
}
})
}
if(tasks.length > 0){
list = (await Promise.all(tasks)).reduce((acc, cur) => {
return {
data: acc.data.concat(cur.data)
}
})
}
const {data} = await axios.get(URL)
if(data.code>=1000){
console.log(data.msg);
return 0
}
const playlist = data.result
console.log("playlist:" + playlist)
const newData = []
for(let i=0, len1=playlist.length; i<len1; i++){
let flag = true
for(let j=0, len2 = list.length; j<=len2; j++){
if (playlist[i].id === list[j].id) {
flag = false
break
}
}
if(flag){
let pl = playlist[i]
pl.createTime = db.serverDate()
newData.push(pl)
}
}
console.log(newData)
console.log([...newData])
// 更新代码: 一次性批量插入数据
if (newData.length > 0) {
await playlistCollection.add({
data: newData
}).then((res) => {
console.log('插入成功')
}).catch((err) => {
console.log(err)
console.error('插入失败')
})
}
return newData.length
}
写回答
3回答
-
同样的代码写了两遍:
012021-04-16 -
幕布斯3491790
提问者
2021-04-15
老师,如果改成list.data,我重新运行一遍,出现如下问题:
代码如下:
// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() const URL = 'https://apis.imooc.com/personalized?icode=CDD8F0D419BEEF3B' const axios = require('axios') const playlistCollection = db.collection('playlist') const MAX_LIMIT = 100 // 云函数入口函数 exports.main = async (event, context) => { // const countResult = await playlistCollection.count() // const list = await playlistCollection.get() const countResult = await playlistCollection.count() const total = countResult.total const batchTimes = Math.ceil(total/MAX_LIMIT) const tasks = [] for (let i = 0; i < batchTimes; i++) { let promise = playlistCollection.skip(i * MAX_LIMIT).limit(MAX_LIMIT).get() tasks.push(promise) } let list = { data: [] } if (tasks.length > 0) { list = (await Promise.all(tasks)).reduce((acc, cur) => { return { data: acc.data.concat(cur.data) } }) } if(tasks.length > 0){ list = (await Promise.all(tasks)).reduce((acc, cur) => { return { data: acc.data.concat(cur.data) } }) } const {data} = await axios.get(URL) if(data.code>=1000){ console.log(data.msg); return 0 } const playlist = data.result console.log("playlist:" + playlist) const newData = [] for(let i=0, len1=playlist.length; i<len1; i++){ let flag = true for(let j=0, len2 = list.data.length; j<=len2; j++){ if (playlist[i].id === list.data[j].id) { flag = false break } } if(flag){ let pl = playlist[i] pl.createTime = db.serverDate() newData.push(pl) } } console.log(newData) console.log([...newData]) // 更新代码: 一次性批量插入数据 if (newData.length > 0) { await playlistCollection.add({ data: newData }).then((res) => { console.log('插入成功') }).catch((err) => { console.log(err) console.error('插入失败') }) } return newData.length }
00 -
谢成
2021-04-15
这里代码有点问题,list是一个对象,list里面的data才是数组,应该改成list.data
00
相似问题