小程序云函数 异步请求

单层请求

复制代码
exports.main = async (event, context) => {
  const {from,to,date,message} = event;await db.collection('chatRoom')
    .where({
      chat:_.all([from,to])
    }).update({
      data:{
        message : _.push({from,to,date,message})
      }
    })
  .then(res => return res)
  .catch(err => return err)
} }
复制代码

 

 

多层请求嵌套使用 try catch

复制代码
// 云函数入口函数
exports.main = async (event, context) => {
  const {from,to,date,message} = event;
  try {

  //第一层 let res
= await db.collection('chatRoom') .where({ chat:_.all([from,to]) }).update({ data:{ message : _.push({from,to,date,message}) } })

  //第二层 会等待第一层结果返回
if(res.stats.updated === 0) { //注意数据库调用返回没有result|云函数调用有 res = await db.collection('chatRoom').add({ data:{ chat: [from,to], messages:[{from,to,date,message}], date:date } }) } return res } catch (error) { return error } }
复制代码

 

posted @   yheyi  阅读(37)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示