Meteor方法错误 - 架构允许[validation-error]

时间:2016-11-09 20:16:30

标签: meteor simple-schema

我收到以下错误,并且不知道为什么我非常确定我正确地做了一切。我传递了一系列对象并对其进行了验证。

方法

export const insert = new ValidatedMethod({
    name: 'lineups.insert',

    // Validation
    validate(args) {
        console.log(args);
        new SimpleSchema({
            business: {  type: [Object] },
            competitors: { type: [Object] },
            location: { type: Object },
            type: { type: String }
        }).validate(args)
    },

    // Run
    run({ lineupsId}) {

        const loggedInUser = Meteor.user();

        // check user roles
        if (!loggedInUser) {
            throw new Meteor.Error(403, 'Access Denied');
        }
    }
}); 

以下是方法获取的arg

Object {business: Array[1], competitors: Array[1], location: Object, type: "Business Analysis"}

但我收到以下错误 架构[validation-error]

不允许business.0.placeID

调用方法

let businessArray = $.map(Session.get('business'), function(value, index) {
            return [{placeID:value.place_id, name:value.name}];
        });

insert.call({
            business:  businessArray,
            competitors:  Session.get('competitors'),
            location: Session.get('location'),
            type:Session.get('type')
        }, (err, res) => {
            if (err) {
                if (ValidationError.is(err)) {
                    toastr.error(err.message);
                }
            } else {
                toastr.error('Success, lineup have been created');
            }
        });

2 个答案:

答案 0 :(得分:3)

所以问题在于您需要验证每个对象值。

可以这样做

validate: new SimpleSchema({
        type: { type: String },
        business: { type: [Object] },
        'business.$.name': { type: String },
        'business.$.placeID': { type: String }
    }).validator(),  

另一种选择是这样的,但是有了这个,我无法让方法运行来执行。

validate(args) {
        new SimpleSchema({
            business: [{ placeID: String, name: String }],
            competitors: { type: [Object] },
            location: { type: Object },
            type: { type: String }
        }).validator(args)
    },

答案 1 :(得分:0)

试试这个,

validate(args) {
    console.log(args);
    new SimpleSchema({
        business: {  blackbox: true, type: [Object] },
        competitors: { blackbox: true, type: [Object] },
        location: { blackbox: true, type: Object },
        type: { type: String }
    }).validate(args)
},