歌单去重云函数报错502005

来源:3-11 歌单数据去重

39sakuchan

2019-08-29

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const db = cloud.database()

const rp = require('request-promise')

const URL = 'http://musicapi.xiecheng.live/personalized'

const platlistCollection = db.collection('platlist')

// 云函数入口函数
exports.main = async (event, context) => {
 const list = await platlistCollection.get()

 const 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 = newData.length; i < len; i++){
    await platlistCollection.add({
      data:{
        ...newData[i],
        createTime: db.serverDate(),
      }
    }).then((res)=>{
      console.log('插入成功')
    }).catch((err)=>{
      console.log('插入失败')
    })
  }

  return newData.length
}

调用状态
失败, ret code 1
返回结果
{“errorCode”:1,“errorMessage”:“user code exception caught”,“stackTrace”:"errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.; "}
调用日志
START RequestId: 5196389e-ca04-11e9-b30a-525400e24e59

Event RequestId: 5196389e-ca04-11e9-b30a-525400e24e59

Error: errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.;

查了下错误码是无权限操作数据库,微信登录的账号也一直是我的,想问下是什么问题

写回答

4回答

谢成

2019-08-29

首先确定该云环境下是否存platlist集合,注意你这里写的集合名称是platlist。

//img1.sycdn.imooc.com/szimg/5d674cbe0949396114620296.jpg然后再检查是否配置云环境id:

1、在云开发控制台中查看云环境id,然后在app.js当中配置云环境id:

//img1.sycdn.imooc.com/szimg/5d674c100977ce9a18040752.jpg

//img1.sycdn.imooc.com/szimg/5d674c1c09f6172f16810833.jpg

2、在初始化数据库中配置云环境id

//img1.sycdn.imooc.com/szimg/5d674c4c09d12ede14100762.jpg

改成: 

 const db = cloud.database({

    env: 'XXXX'

})



0
1
39sakuchan
非常感谢!
2019-08-29
共1条回复

笃爱直播

2019-12-15

我解决了

调用状态

成功

返回结果

30

调用日志


0
0

笃爱直播

2019-12-15

我按老师说的改了 还是失败?请指导

0
0

笃爱直播

2019-12-15

// 云函数入口文件

const cloud = require('wx-server-sdk')


cloud.init()


const db = cloud.database({


    env: 'test-2ywfr'


})


const rp = require('request-promise')


const URL = 'http://musicapi.xiecheng.live/personalized'


const platlistCollection = db.collection('piaylist')


// 云函数入口函数

exports.main = async (event, context) => {

 const list = await platlistCollection.get()


 const 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 = newData.length; i < len; i++){

    await platlistCollection.add({

      data:{

        ...newData[i],

        createTime: db.serverDate(),

      }

    }).then((res)=>{

      console.log('插入成功')

    }).catch((err)=>{

      console.log('插入失败')

    })

  }


  return newData.length

}


测试云函数 “getPlaylist”

云端测试无法获取用户登陆态信息。

测试模版

新建模版

1

2

3

4

{

 

}

 

运行测试

云函数名称

getPlaylist

开始时间

2019-12-15 16:07:43

时长

54.02ms

计费时长

100ms

调用完成

内存使用

39.35 MB

Request ID

f8bdbaea-1f11-11ea-a02e-5254007aa7a1

调用状态

失败, ret code 1

返回结果

{"errorCode":1,"errorMessage":"user code exception caught","stackTrace":"errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.; \nError: errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.; \n    at new CloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6398:28)\n    at Object.returnAsCloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6450:16)\n    at Object.checkError (/var/user/node_modules/wx-server-sdk/index.js:1680:23)\n    at CollectionReference.\u003canonymous\u003e (/var/user/node_modules/wx-server-sdk/index.js:1841:41)\n    at step (/var/user/node_modules/tslib/tslib.js:136:27)\n    at Object.next (/var/user/node_modules/tslib/tslib.js:117:57)\n    at fulfilled (/var/user/node_modules/tslib/tslib.js:107:62)\n    at \u003canonymous\u003e\n    at process._tickCallback (internal/process/next_tick.js:188:7)"}

调用日志

START RequestId: f8bdbaea-1f11-11ea-a02e-5254007aa7a1

Event RequestId: f8bdbaea-1f11-11ea-a02e-5254007aa7a1

Error: errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.;

at new CloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6398:28)

at Object.returnAsCloudSDKError (/var/user/node_modules/wx-server-sdk/index.js:6450:16)

at Object.checkError (/var/user/node_modules/wx-server-sdk/index.js:1680:23)

at CollectionReference.<anonymous> (/var/user/node_modules/wx-server-sdk/index.js:1841:41)

at step (/var/user/node_modules/tslib/tslib.js:136:27)

at Object.next (/var/user/node_modules/tslib/tslib.js:117:57)

at fulfilled (/var/user/node_modules/tslib/tslib.js:107:62)

at <anonymous>

at process._tickCallback (internal/process/next_tick.js:188:7)

END RequestId: f8bdbaea-1f11-11ea-a02e-5254007aa7a1

Report RequestId: f8bdbaea-1f11-11ea-a02e-5254007aa7a1 Duration:54ms Memory:256MB MaxMemoryUsed:39.347656MB


0
1
谢成
你好,从错误信息上看也是集合不存在,请按照上面的步骤,检查下正在操作的集合是否存在。
2019-12-17
共1条回复

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

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

1938 学习 · 2768 问题

查看课程