请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

歌单去重云函数报错502005

// 云函数入口文件
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回答

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

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

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

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

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

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

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

改成: 

 const db = cloud.database({

    env: 'XXXX'

})



0 回复 有任何疑惑可以回复我~
  • 提问者 39sakuchan #1
    非常感谢!
    回复 有任何疑惑可以回复我~ 2019-08-29 11:58:43
笃爱直播 2019-12-15 16:35:27

我解决了

调用状态

成功

返回结果

30

调用日志


0 回复 有任何疑惑可以回复我~
笃爱直播 2019-12-15 16:11:21

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

0 回复 有任何疑惑可以回复我~
笃爱直播 2019-12-15 16:08:32

// 云函数入口文件

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 09:35:07
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信