本地插入数据失败
来源:3-10 优化接口请求方式及数据批量插入
weixin_慕侠1417090
2023-04-02
图片描述
// 云函数入口文件
const cloud = require(‘wx-server-sdk’)
cloud.init()
const db = cloud.database()
const axios = require(‘axios’)
const URL = ‘https://apis.imooc.com/personalized?icode=EB9FE70AB…’
const playlistCollection = db.collection(‘playlist’)
// const rp = require(‘request-promise’);
// 云函数入口函数
exports.main = async (event, context) => {
const {
data
} = await axios.get(URL)
if (data.code >= 1000) {
console.log(data.msg)
return 0
}
const playlist = data.result
// 更新代码: 一次性批量插入数据
if (playlist.length>0) {
await playlistCollection.add({
data: [...playlist]
}).then((res) => {
console.log('插入成功')
}).catch((err) => {
console.log(err)
console.error('插入失败')
})
}
}
2回答
-
谢成
2023-04-03
云函数中设置云环境id:
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV})
然后重新上传云函数。
00 -
weixin_慕侠1417090
提问者
2023-04-02
Error: collection.add:fail-501007 是游客身份没有登录导致的,登录之后报错Error: collection.add:fail -501001
Error: collection.add:fail -501001 resource system error. Environment not found, there is no default environment exists, please explicitly specify the environment
at Object.toSDKError (D:\WXAppProject\music-imooc\cloudfunctions\getPlaylist\node_modules\wx-server-sdk\index.js:8094:19)
at Object.checkError (D:\WXAppProject\music-imooc\cloudfunctions\getPlaylist\node_modules\wx-server-sdk\index.js:1422:23)
at D:\WXAppProject\music-imooc\cloudfunctions\getPlaylist\node_modules\wx-server-sdk\index.js:1210:33
at processTicksAndRejections (node:internal/process/task_queues:96:5)00
相似问题
回答 1
回答 1