gpt4 book ai didi

meteor - MDG ValidatedMethod 与 Aldeed Autoform : "_id is not allowed by the schema" error

转载 作者:行者123 更新时间:2023-12-02 05:43:09 25 4
gpt4 key购买 nike

当尝试使用自动表单通过 ValidatedMethod 更新集合时,我收到错误“架构不允许_id”。

据我所知this exampleofficial docs我的架构不期望包含 _id 字段,并且我不期望从更新语句中更新 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>

模板“代码隐藏”: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);

最佳答案

我终于弄清楚了这件事的真相。问题是 autoform 传入一个复合对象,该对象表示要更改的记录的 id 以及数据的修饰符 ($set),而不仅仅是数据本身。所以该对象的结构如下:

_id: '5TTbSkfzawwuHGLhy',
modifier:
{
'$set':
{ name: 'Smiths Fabrication Ltd',
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd5d2dad3fccfd1d5c8d4cfdaddde92dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>',
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);
}
});

关于meteor - MDG ValidatedMethod 与 Aldeed Autoform : "_id is not allowed by the schema" error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35066568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com