云函数index.js
exports.main = async (event, context) => {
try {
// 构建学生信息文档
const school = event.school;
const stu_name = event.stu_name;
const stu_code = event.stu_code;
const stu_phone_num = event.stu_phone_num;
const file_id = event.file_id;
const creat_time = new Date();
// 保存学生信息到student_info集合
const result = await db.collection('student_info')
.add({
school: school,
stu_name: stu_name,
stu_code: stu_code,
stu_phone_num: stu_phone_num,
file_id: file_id,
creat_time: creat_time
});
console.log("保存信息成功");
console.log(result);
return {
success: true,
message: '信息保存成功',
insertedId: result._id, // 返回插入文档的ID
};
} catch (error) {
console.error('Error occurred while saving student info:', error);
return {
success: false,
message: '信息保存失败',
};
}
};
页面调用.js
//保存学生信息
saveinfo() {
wx.cloud.callFunction({
name: 'saveStuInfo',
data: {
school:this.data.school,
stu_name:this.data.stu_name,
stu_code:this.data.stu_code,
stu_phone_num:this.data.stu_phone_num,
file_id:this.data.file_id,
},
success: (res) => {
const {
success,
message
} = res.result;
if (success) {
wx.showToast({
title: message,
icon: 'success',
duration: 2000,
mask: 'true',
});
} else {
wx.showToast({
title: message,
icon: 'error',
duration: 2000,
mask: 'true',
})
}
}
});
},
保存后,数据库中没有数据怎么回事?
.add({data:{...}})