使用Aldeed Autoform的MDG ValidatedMethod:"架构不允许_id;#34;错误

时间:2016-01-28 16:18:58

标签: meteor meteor-blaze meteor-autoform meteor-collection2 simple-schema

我收到错误"架构不允许_id;#34;尝试使用autoform通过ValidatedMethod更新集合时。

据我所知this exampleofficial docs,我的架构没有期望包含_id字段,我也不希望更新来自更新语句,所以我不知道为什么会发生这种错误。

如果我从使用经过验证的方法切换到直接写入集合(附加到没有id的集合的模式),一切都按预期工作,所以我假设这个问题是我的ValidatedMethod中的验证。

知道我做错了吗?

模板:customer-edit.html

<template name="updateCustomerEdit">
    {{> quickForm
        collection="CustomerCompaniesGlobal"
        doc=someDoc
        id="updateCustomerEdit"
        type="method-update"
        meteormethod="CustomerCompanies.methods.update"
        singleMethodArgument=true
    }}
</template>

模板&#39;代码&#39;:customer-edit.js

Template.updateCustomerEdit.helpers({

    someDoc() {
        const customerId = () => FlowRouter.getParam('_id');
        const instance = Template.instance();

        instance.subscribe('CustomerCompany.get', customerId());

        const company = CustomerCompanies.findOne({_id: customerId()});
        return company;
    }
});

更新验证方法:

// The update method
update = new ValidatedMethod({

    // register the name
    name: 'CustomerCompanies.methods.update',

    // register a method for validation, what's going on here?
    validate: new SimpleSchema({}).validator(),

    // the actual database updating part validate has already been run at this point
    run( newCustomer) {
    console.log("method: update");
        return CustomerCompanies.update(newCustomer);
    }
});

架构:

Schemas = {};

Schemas.CustomerCompaniesSchema = new SimpleSchema({

    name: {
        type: String,
        max: 100,
        optional: false
    },

    email: {
        type: String,
        max: 100,
        regEx: SimpleSchema.RegEx.Email,
        optional: true
    },

    postcode: {
        type: String,
        max: 10,
        optional: true
    },

    createdAt: {
        type: Date,
        optional: false
    }
});

收集:

class customerCompanyCollection extends Mongo.Collection {};

// Make it available to the rest of the app
CustomerCompanies = new customerCompanyCollection("Companies");
CustomerCompaniesGlobal = CustomerCompanies;

// Deny all client-side updates since we will be using methods to manage this collection
CustomerCompanies.deny({
    insert() { return true; },
    update() { return true; },
    remove() { return true; }
});

// Define the expected Schema for data going into and coming out of the database
//CustomerCompanies.schema = Schemas.CustomerCompaniesSchema

// Bolt that schema onto the collection
CustomerCompanies.attachSchema(Schemas.CustomerCompaniesSchema);

2 个答案:

答案 0 :(得分:4)

我终于到底了。问题是autoform传递一个复合对象,它表示要更改的记录的id以及数据的修饰符($ set),而不仅仅是数据本身。所以该对象的结构如下:

_id: '5TTbSkfzawwuHGLhy',
modifier:
{ 
  '$set':
    { name: 'Smiths Fabrication Ltd',
      email: 'info@smithsfab.com',
      postcode: 'OX10 4RT',
      createdAt: Wed Jan 27 2016 00:00:00 GMT+0000 (GMT Standard Time) 
    } 
} 

一旦我想到这一点,我将更新方法更改为此,然后一切按预期工作:

// Autoform specific update method that knows how to unpack the single
// object we get from autoform.
update = new ValidatedMethod({

    // register the name
    name: 'CustomerCompanies.methods.updateAutoForm',

    // register a method for validation.
    validate(autoformArgs) {
        console.log(autoformArgs);
        // Need to tell the schema that we  are passing in a mongo modifier rather than just the data.
        Schemas.CustomerCompaniesSchema.validate(autoformArgs.modifier , {modifier: true});
    },

    // the actual database updating part
    // validate has already been run at this point
    run(autoformArgs)
    {
        return CustomerCompanies.update(autoformArgs._id, autoformArgs.modifier);
    }
});

答案 1 :(得分:1)

优异。当我努力寻找有关该主题的任何其他信息时,您的帖子帮助了我。

为了建立您的答案,如果由于某种原因您希望将表单数据作为单个块获得,则可以在AutoForm中使用以下内容。

type="method" meteormethod="myValidatedMethodName"

您的验证方法可能如下所示:

export const myValidatedMethodName = new ValidatedMethod({
  name: 'Users.methods.create',
  validate(insertDoc) {
    Schemas.NewUser.validate(insertDoc);
  },
  run(insertDoc) {
    return Collections.Users.createUser(insertDoc);
  }
});

注意:Schema.validate()方法需要一个Object,而不是以前的修饰符。

我不清楚这两种方法是否有任何明显的优势。

type =&#34; method-update&#34;显然是你想要更新文档的方式,因为你得到了修饰符。 type =&#34;方法&#34;似乎是创建新文档的最佳方式。在大多数情况下,如果您不打算从表单数据创建文档,这可能也是最佳选择。