本来写完的项目 复制了份代码 云数据库 也复制了 云函数啥的都复制了一份 导入改好appid后 把环境ID也改好了 写测试demo也能正常访问数据库 云函数也写了demo能正常使用
但是前面的云函数就调用失败
云函数代码
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const $ = db.command.aggregate
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
let location = event.location
let maxDistance = event.maxDistance
let bdata,cdata=[]
let adata=[]
let type = event.type
let postdata = []
if(type=="recommend"){
postdata = await db.collection('recruiter_post_lease').aggregate().match({
_id:_.in(event.a),
status:'doing'
}).lookup({
from:'find_servicer',
localField: '_openid',
foreignField: '_openid',
as:'pInfo'
}).unwind('$pInfo').lookup({
from:'find_companysInfo',
localField:'pInfo.companyID',
foreignField:'_id',
as:'cInfo'
}).unwind('$cInfo').addFields({
location:"$cInfo.location"
}).limit(100)
.end().then(res => {return res.list})
}else if(type=="time"){
postdata = await db.collection('recruiter_post_lease').aggregate().match({
status:'doing'
}).sort({
createTime:-1
}).lookup({
from:'find_servicer',
localField: '_openid',
foreignField: '_openid',
as:'pInfo'
}).unwind('$pInfo').lookup({
from:'find_companysInfo',
localField:'pInfo.companyID',
foreignField:'_id',
as:'cInfo'
}).unwind('$cInfo').addFields({
location:"$cInfo.location"
}).limit(100)
.end().then(res => {return res.list})
}
postdata.forEach(el => {
if(el.pInfo.status == '审核通过'){
adata.push(el)
}
})
if(maxDistance){
await db.collection('recruiter_post_lease').aggregate()
.geoNear({
distanceField: 'distance',
spherical: true,
near: location,
maxDistance:maxDistance
}).end().then(res => {bdata=res.list})
}else{
await db.collection('recruiter_post_lease').aggregate()
.geoNear({
distanceField: 'distance',
spherical: true,
near: location,
}).end().then(res => {bdata=res.list})
}
adata.forEach(element => {
bdata.forEach(another => {
if(element._id == another._id){
let distance = another.distance/1000
let ddata = {
element,
distance:distance.toFixed(1)
}
cdata.push(ddata)
}
})
})
return {
postdata:cdata
}
}
js调用代码
getPostdata3:function(type){
wx.showLoading({
title: '数据快到啦',
})
let lct = this.data.userLocation
wx.cloud.callFunction({
name:'getClosePostdata_3',
data:{
//传过去的这三个都是有数据的
a:this.data.Recommend,
type:type,
location:db.Geo.Point(lct.longitude,lct.latitude)
},
success: res =>{
this.setData({
postData:res.result.postdata
})
wx.setStorage({
data: res.result.postdata,
key: 'postdatas',
})
wx.hideLoading()
},
fail: err =>{
console.log('获取失败:'+err)
wx.hideLoading()
}
})
},
同样遇到了 你解决了吗