调用云函数时,已经上传了云函数,但提示找不到对应的FunctionName 或提示数据库或数据集合未定义的问题:
官方文档里没有提到,百思不得其解。最后在微信开放社区提问找到了答案↓
原因:小程序可能存在多个云环境,如果在调用云函数(wx.cloud.callFunction)的时候未指定云环境则会导致小程序找不到云函数入口
解决如下:在调用时声明云环境,即加上config
1 wx.cloud.callFunction({ 2 config:{ env: '环境ID' } 3 ... 4 })
完整添加函数如下:
1 wx.cloud.callFunction({ 2 config:{ env: 'public-i10xo' }, 3 name:'cloudAdd', 4 data:{ 5 bh:list[i].bianhao, 6 xingh:list[i].xinghao, 7 wd:wangdian, 8 xuh:noteIndex, 9 sj:nowdate, 10 }, 11 success:function(res){ 12 console.log(res) 13 } 14 })
对应的云函数:
1 // 云函数入口文件 2 const cloud = require('wx-server-sdk') 3 4 cloud.init({ 5 env:cloud.DYNAMIC_CURRENT_ENV 6 })//这句也很重要!! 7 8 const db = cloud.database() 9 // 云函数入口函数 10 exports.main = async (event, context) => { 11 const wxContext = cloud.getWXContext() 12 return await db.collection('List').add({ 13 data:{ 14 编号:event.bh, 15 型号:event.xingh, 16 网点:event.wd, 17 序号:event.xuh, 18 时间:event.sj, 19 } 20 21 }) 22 }
附上删除函数:
1 wx.cloud.callFunction({ 2 config:{ env: 'public-i10xo' }, 3 name:"xingRemove", 4 data:{ 5 currentv:currentvalue 6 }, 7 success:res=>{ 8 console.log(res) 9 wx.showToast({ 10 title: '删除成功', 11 icon:'none', 12 duration: 2000 13 }) 14 }, 15 fail:err=>{ 16 console.log(err) 17 } 18 })
对应的云函数:
// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init({ env:cloud.DYNAMIC_CURRENT_ENV }) const db = cloud.database() // 云函数入口函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext() return await db.collection('XingHao').where({ 型号:event.currentv }).remove() }
修改函数:
1 wx.cloud.callFunction({ 2 config:{ env: 'public-i10xo' }, 3 name:"xingUpdate", 4 data:{ 5 oldv:old, 6 newv:input 7 }, 8 success:res=>{ 9 console.log(res) 10 wx.showToast({ 11 title: '修改成功', 12 icon:'none', 13 duration: 2000 14 }) 15 }, 16 fail:err=>{ 17 console.log(err) 18 } 19 })
对应的云函数:
1 // 云函数入口文件 2 const cloud = require('wx-server-sdk') 3 4 cloud.init({ 5 env:cloud.DYNAMIC_CURRENT_ENV 6 }) 7 8 const db = cloud.database() 9 // 云函数入口函数 10 exports.main = async (event, context) => { 11 const wxContext = cloud.getWXContext() 12 13 return await db.collection('XingHao').where({ 14 型号:event.oldv 15 }) 16 .update({ 17 data: { 18 型号: event.newv 19 }, 20 }) 21 }
运行成功,感谢大佬回答!!
提问传送门: https://developers.weixin.qq.com/community/develop/doc/0008c05375060007bdba4380256000