- 哪位大神给看看,分析一下,谢了!
- 控制台中打印的RES
- {errMsg: "cloud.callFunction:ok", result: {…}, requestID: "4bec6a19-b6cf-11ea-a3d6-525400dd5846"}
- errMsg: "cloud.callFunction:ok"
- requestID: "4bec6a19-b6cf-11ea-a3d6-525400dd5846"
- result:
- errMsg: "collection.add:ok"
- _id: "65536a51795c1cc5cd9d5f9fd1164d50"
- __proto__: Object
- __proto__: Object
新添加的记录截图:
云函数代码:
// 云函数入口文件
const cloud = require('wx-server-sdk')
const crypto = require('crypto-js')
cloud.init({env: cloud.DYNAMIC_CURRENT_ENV})
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var hash_open_id = crypto.MD5(wxContext.OPENID).toString()
console.log(hash_open_id)
if (event.add_user) {
try {
return await db.collection('users').add({
data: {
_id: hash_open_id,
user_info: event.user_info,
address: [],
company_info: {},
trial_list: [],
message_list: [],
isAdmin: 0
}
})
} catch (error) {
console.error(error)
return error
}
} else if (event.get_user) {
try {
return await db.collection("users").where({
_id:hash_open_id
}).get()
} catch (error) {
console.error(error)
return error
}
} else {
//to-do: update profile
}
}
可以返回ID,就足够用了
https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/collection/Collection.add.html
文档中有说明,仅返回对象 ID
不带。