云函数调用数据库插入数据,云函数调用成功,没有报错,但是数据没插入是为什么?
下面是云函数代码
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,// 使用当前云环境
})
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
// const { _openid, userName, userImage } = event
try {
return await db.collection('accounts').where({
_openid: _.eq(event._openid)
// _openid: event._openid // 填入当前用户 openid
}).get({
success: function (res) {
if (res.data.length == 0) {
db.collection('accounts').add({
data: {
_openid: event._openid,
userName: event.userName,
userImage: event.userImage
},
success: res => {
}
})
}
},
fail: console.error
})
} catch (e) {
return e
}
}
// 云函数入口文件 const cloud = require('wx-server-sdk') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV,// 使用当前云环境 }) const db = cloud.database() const _ = db.command const wxContext = cloud.getWXContext() const openId = wxContext.OPENID // 云函数入口函数 exports.main = async (event, context) => { try { return await db.collection('accounts').where({ _openid: openId // _openid: event._openid // 填入当前用户 openid }).get() .then(res => { var length = res.data.length if(res.data.length == 0){ db.collection('accounts').add({ data: { _openid: event._openid, userName: event.userName, userImage: event.userImage }, }) } }) }
exports.main = async (event, context) => { let res = await xxx.get() if (res.data.length == 0) {...} }
db.collection('accounts').where({...})..get({success:function(res){...}...})
你这是小程序端的写法放到了云函数端吧,再仔细看下文档吧
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/collection/Collection.get.html