HawkLu92
HawkLu92
  • 发布:2021-12-09 17:58
  • 更新:2021-12-10 11:56
  • 阅读:600

【报Bug】uni-cloud-router controller 校验参数bug

分类:uniCloud

产品分类: uniCloud/腾讯云

示例代码:

controller如下:

"use strict"  
const { Controller } = require("uni-cloud-router");  
module.exports = class AddressController extends Controller {  
    // 新增  
       async add(){  
            const { ctx, service } = this;  
            // 校验参数  
            ctx.validate({  
                name: { type: "string" }  
            });  
            // 组装参数  
            const post = Object.assign(ctx.data,{user_id:ctx.auth.uid});  
            // // 调用 Service 进行业务处理  
            return service.address.add(post);  

  }  

};

本地param参数如下:

{  
    "action":"address/add",  
    "data": {  
        "name":"李四"  
    },  
    "uniIdToken":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiIxOGVkMDk2ODYxOTMxMmI5MDU4MmIzOTA3MGJjMmM0OSIsInJvbGUiOltdLCJwZXJtaXNzaW9uIjpbXSwiaWF0IjoxNjM5MDEzODYxLCJleHAiOjE2MzkyNzMwNjF9.TRP777OkBDmqHEMWej_sp13hgU8tdIm1GRnOvFdWfa0"  
}

service文件内容如下:

"use strict"  
const { Service } = require("uni-cloud-router");  
module.exports = class AddressService extends Service {  
  async add(data) {  
    return this.db.collection("uni-id-address").add(data)  
  }  
};

操作步骤:

先用正确的本地参数,运行本地云函数正常,然后将本地参数name字段改为Number后报错,再改回正常后出现bug

预期结果:

能正确校验

实际结果:

异常报错

bug描述:

前几次次校验正常,专门写了个本地校验不通过的param.json 校验过后报如下异常:

{  
    "code": "INVOKE_FUNCTION_FAILED",  
    "message": "ctx.validate is not a function",  
    "stack": "TypeError: ctx.validate is not a function\n    at Proxy.add (/Users/dafei/luliang/workspace/zhizu/zhizu/uniCloud-tcb/cloudfunctions/router/controller/address.js:8:8)\n    at a (/Users/dafei/luliang/workspace/zhizu/zhizu/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:9940)\n    at i (/Users/dafei/luliang/workspace/zhizu/zhizu/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:6247)\n    at auth (/Users/dafei/luliang/workspace/zhizu/zhizu/uniCloud-tcb/cloudfunctions/router/middleware/auth.js:13:11)\n    at processTicksAndRejections (internal/process/task_queues.js:97:5)\n    at async R (/Users/dafei/luliang/workspace/zhizu/zhizu/uni_modules/uni-cloud-router/uniCloud/cloudfunctions/common/uni-cloud-router/dist/index.js:1:7655)"  
}
2021-12-09 17:58 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com

刚在学习router的使用 按post例程也出现了这个错误 蹲个结果

DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

ctx.validate是需要自己的中间件实现的方法,包括文档这段代码下面的ctx.auth.uid也是需要用户自己实现中间件挂载的。我们修改一下文档

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