去重不成功 每次都添加到云数据库中
来源:3-11 歌单数据去重
weixin_慕工程3405104
2020-06-14
// 云函数入口文件
const cloud = require(‘wx-server-sdk’)
cloud.init()
let db = cloud.database({
env:“xxx”
})
const rp = require(‘request-promise’);
const URL = "http://musicapi.xiecheng.live/personalized"
const playlistCollection = db.collection(“playlist”)
// 云函数入口函数
exports.main = async (event, context) => {
let list =await playlistCollection.get()
let playlist =await rp(URL).then((res)=>{
return JSON.parse(res).result
})
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){
newData.push(playlist[i])
}
}
for(let i = 0 , len = playlist.length; i < len ; i++){
await playlistCollection.add({
data:{
…playlist[i],
createTime:db.serverDate()
}
}).then((res)=>{
console.log(“success”)
}).catch((err)=>{
console.error(“err”)
})
}
return newData.length
}
1回答
-
weixin_慕工程3405104
提问者
2020-06-14
已解决 添加时候的问题
012020-07-20
相似问题