美文网首页meteor 开发者
meteor前端调用服务端方法常见错误以及问题

meteor前端调用服务端方法常见错误以及问题

作者: danihay | 来源:发表于2017-10-11 10:35 被阅读7次

    1.传的参数在数据库方法中没有校验类型

    image.png
          validate: new SimpleSchema({
                userId: {
                    type: String,
                },
            }).validator(),
       run({ userId, condition }) {
    在该方法中只校验了userId,没有校验condition
    

    当按以下方法校验了condition,会发现还会一级级报错,如下图。

    condition: {
         type: Object,
    }
    
    image.png
    解决方案1 : condition: {
                    type: Object,
                    blackbox: true,  //只要写入这个参数就不会一级级去校验了
                    optional: true,
                },
    

    2.数据表结构书写时,没有设置可选项,传参时没有传入则会报以下错误。

    image.png
    解决方案1 :传入downloadAt参数
    解决方案2 :downloadAt: {
            label: '下载时间',
            type: String,
            optional: true, (设置其为可选)
        },
    

    3.服务端调用find,但是前端没有返回的数据,find只负责匹配选项,不负责获取数据

    .fetch()可以获取查找到的数据
    run({ userId }) {
                return Collections.DownloadRecords.find({ userId }).fetch();
            },
    

    4.回调函数使用及执行,callback最后一个参数

    (1)actions文件夹中
    findDownloadRecords({ _Meteor }, callback) {
                _Meteor.call('DownloadRecords.methods.findAllRecords', { userId: Meteor.userId() }, (err, result) => {
                    callback(err, result);
                });
            }, 
    (2)components文件夹中调用findDownloadRecords方法
    this.props.findDownloadRecords((err, result) => {
                if (err) {
                    console.log(err);
                }
                console.log(result);    
            }); 
    
    1. 404 not found,BoxInfo.methods.xianKuServerStockUp方法和BoxInfo.js都已存在,需要在index.js引入。
    errorClass {error: 404, reason: "Method 'BoxInfo.methods.xianKuServerStockUp' not found", details: undefined, message: "Method 'BoxInfo.methods.xianKuServerStockUp' not found [404]", errorType: "Meteor.Error", …}
    

    6.error: 500 (findOne,find,update等)数据库(lib/collection)错误。

    Exception while invoking method 'GuestOrder.methods.confirmOrder' TypeError: Cannot read property 'findOne' of undefined
    at Invite.findOne (server/methods/invite/interface.js:5:37)
    

    相关文章

      网友评论

        本文标题:meteor前端调用服务端方法常见错误以及问题

        本文链接:https://www.haomeiwen.com/subject/algiyxtx.html