只在真机调试时 云函数才出现调用失败
发布于 5 年前 作者 weidong 8340 次浏览 来自 问答
  • 此为真机调试时出现的 bug

错误代码为 -404006

在开发者工具中则不会出现此种错误

  • 预期表现
  • 复现路径
  • 提供一个最简复现 Demo

云函数名称为 `functions/booklist/indexjs`:

```js

// 云函数入口文件

const cloud = require(‘wx-server-sdk’)

cloud.init()

// 云函数入口函数

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

const { page: offset } = event

const limit = 10 // 限制

const skip = offset * limit // 分页

try {

const db = cloud.database()

const col = db.collection(‘books’)

return await col.skip(skip).limit(limit).orderBy(‘addDate’, ‘desc’).get()

} catch (error) {

return {

code: -1,

data: {

msg: `获取列表失败 ${error}`

}

}

}

}

```

接口调取逻辑:

```js

async getBookList (init) { // 增加 init 参数,如果 init 为 true 则永远加载第一页

console.log('loading ', this.page)

wx.showNavigationBarLoading()

wx.showLoading({

title: Loading…,

mask: true

})

if (init) {

this.page = 0

this.ifNoMoreData = false // 若初始化则仍然有数据

}

let res

try {

res = await wx.cloud.callFunction({

name: booklist,

data: { page: this.page }

})

res = res.result.data

// 数据获取成功

if (res.length < 10 && this.page > 0) { // 如果数据不足 10 条则配置 ifNoMoreData 为 true

this.ifNoMoreData = true

}

} catch (error) {

console.log('call func ', error)

}

// 处理下拉刷新

if (init) {

this.booklist = res

wx.stopPullDownRefresh() // 关闭下拉刷新

} else {

// 处理触底加载

this.booklist = this.booklist.concat(res)

}

wx.hideLoading()

wx.hideNavigationBarLoading()

},

```

回到顶部