修坦克
修坦克
  • 发布:2021-05-22 08:44
  • 更新:2021-05-24 09:22
  • 阅读:405

云函数action如何在before环节抛出错误跳过数据库操作?

分类:uniCloud

按照官方文档说明,
action在任一before环节抛出错误直接进入after流程。
action-b.before->执行数据库操作->action-b.after
也就是说抛出错误就会跳过执行数据库操作

但是我怎么写throw都会执行数据库操作。

求详细写法

2021-05-22 08:44 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

代码贴一下

  • 修坦克 (作者)

    action


    checkdepartment.js


    module.exports = {  
    before: async (state, event) => {
    console.log(state.auth.uid);
    const db=uniCloud.database()
    const collection=db.collection('uni-id-users').where({
    '_id':state.auth.uid
    }).get()
    .then((res)=>{
    console.log(res);
    if(res.data[0].department_id=="cy0001"){
    console.log("ok");
    }else{
    throw new Error('TOKEN_INVALID')
    }
    }).catch(err=>{
    console.log(err)
    })
    },
    after: async (state, event, error, result) => {
    console.log("!!!!!"+error)
    if (error) {
    throw error
    }
    return result
    }
    }

    unicloud-db


    <unicloud-db ref="udb" collection="opendb-device" field="department_id,model,type"  
    v-slot:default="{data,pagination,loading,error,options}" action="checkdepartment"
    :options="options" :where="where" @load="handleLoad" :page-size="size">

    结果还是会读取数据库并显示

    2021-05-24 09:46

  • DCloud_uniCloud_WYQ


    • async、promise的用法还需要自己研究一下

    • 你这错误自己catch了


    const res = await db.collection('uni-id-users').where({  
    '_id':state.auth.uid
    }).get()
    if(res.data[0].department_id=="cy0001"){
    console.log("ok");
    }else{
    throw new Error('TOKEN_INVALID')
    }

    2021-05-24 10:52

  • 修坦克 (作者)

    回复 DCloud_uniCloud_WYQ: 感谢,搞定了。

    我是后端新人。学了不到一个礼拜。见笑了。

    unicloud真的挺强的。

    2021-05-24 11:56

该问题目前已经被锁定, 无法添加新回复