const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
try {
return await db.collection(event.tableName).add({
data: {
_openid:openId,
txt1: value1,
txt2: value2,
...
...
txtn: valuen
}
})
} catch (e) {
console.log(e)
}
}
//如果,我在JS页面调用云函数添加数据库条目,txt1.txt2.txt3等的数量是不确定的,被添加的数据库名也是不一样的,但是我想用一个云函数通配ADD的功能,要如何实现呢?
let data = {} if(txt1) data = {...data,txt1} if(txt2) data = {...data,txt2} let col = db.collection(colName) col.add({data})
vconst cloud = require('wx-server-sdk')
cloud.init({
env: '*********' //填写自己的云开发环境ID
})
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
var otherData = event.dataOther
try {
return await db.collection(event.tableName).add({
data:otherData
})
} catch (e) {
console.log(e)
}
}
js端:
wx.cloud.callFunction({
name: "addData",
data: {
tableName: "workList",
dataOther: {
//txt:"value",
},
},
success: function (res) {
console.log(res)
}
})
这样就完美搞定了