Mongodb 使用一个autoForm,我需要将数据插入到两个集合中

Mongodb 使用一个autoForm,我需要将数据插入到两个集合中,mongodb,meteor,coffeescript,pug,meteor-autoform,Mongodb,Meteor,Coffeescript,Pug,Meteor Autoform,我目前正在开发一个库存系统,该系统将零件收集和采购收集作为应用程序的主干。每个零件都有相应的采购。也就是说,一个零件必须有一个与之相关联的零件号、序列号和成本号。我将Meteor.js与咖啡纸条、翡翠和Graphr一起使用。我可以单独插入每个集合,但它们似乎没有关联。我已经设置了两个连接之间的链接器,但我有点不知道下一步该去哪里 下面是这些集合的一个片段 采购集合 PurchaseInventory.schema = new SimpleSchema partId:

我目前正在开发一个库存系统,该系统将零件收集和采购收集作为应用程序的主干。每个零件都有相应的采购。也就是说,一个零件必须有一个与之相关联的零件号、序列号和成本号。我将Meteor.js与咖啡纸条、翡翠和Graphr一起使用。我可以单独插入每个集合,但它们似乎没有关联。我已经设置了两个连接之间的链接器,但我有点不知道下一步该去哪里

下面是这些集合的一个片段

采购集合

    PurchaseInventory.schema = new SimpleSchema
        partId:
            type:String
            optional:true

        serialNum:
            type:Number
            optional:true

        costNum:
            type:Number
            optional:true
零件集合/模式


    Inventory.schema = new SimpleSchema

        name:
            type:String
            optional:true

        manufacturer:
            type:String
            optional:true

        description:
            type:String
            optional:true

零件查询


    export getInventory = Inventory.createQuery('getInventory',
        $filter: ({ filters, options, params }) ->
                if params.filters then Object.assign(filters, params.filters)
                if params.options then Object.assign(options, params.options)
                return { filters, options , params }
        name:1
        manufacturer:1
        description:1
        pic:1
        purchase:
            partId:1

    )
采购查询


    export getPurchase = PurchaseInventory.createQuery('getPurchase',
        $filter: ({ filters, options, params }) ->
                if params.filters then Object.assign(filters, params.filters)
                if params.options then Object.assign(options, params.options)
                return { filters, options , params }
        serial:1
        cost:1
        date:1
        warrentyDate:1
        userId:1
        )

链接器

//Parts
    Inventory.addLinks
        purchase:
            collection:PurchaseInventory
            inversedBy:"part"

最后是Jade/Pug自动表单


    +autoForm(class="inventoryForm" schema=schema  id="inventoryInsertForm" validation="blur" type="method" meteormethod="inventory.insert")
        .formGroup
          +afQuickField(name="name" label="Name")
          +afQuickField(name="manufacturer" label="Manufacturer")
          +afQuickField(name="description" label="Description")
          button#invenSub(type="submit") Submit


重申一下,我的目标是让每个项目都有一个到其相应购买数据的链接。

最直接的方法是为提交事件使用并创建一个自定义事件处理程序(或者,您可以使用submit上的AutoForm钩子
)。从那里,您可以使用获取当前文档

由于我不喜欢Coffeescript,我将提供以下Blaze/JS代码,但我认为它应该给您一个想法:

{{{#autoForm type=“normal”class=“class=“inventoryForm”schema=schema id=“inventoryInsertForm”validation=“blur”schema=schema id=“insertForm”validation=“blur”}
{{/autoForm}
/**
*根据给定架构验证表单并返回
*包括所有表格数据的相关文件。
*见:https://github.com/aldeed/meteor-autoform#sticky-验证错误
**/
export const formIsValid=函数formIsValid(formId,schema){
const{insertDoc}=AutoForm.getFormValues(formId)
//创建验证上下文
const context=schema.newContext()
验证(insertDoc,选项)
//获取可能的验证错误
//并将它们直接附加到表单上
const errors=context.validationErrors()
if(errors&&errors.length>0){
errors.forEach(err=>AutoForm.addStickyValidationError(formId、err.key、err.type、err.value))
返回空
}否则{
返回insertDoc
}
}
Template.yourFormTempalte.events({
“提交#插入表单”(事件){
event.preventDefault()//防止重新加载页面非常重要!
//验证aginst两个架构以引发验证
//两个而不是其中一个的错误
const insertDoc=formIsValid('insertForm',PurchaseInventory.schema)&&formIsValid('insertForm',Inventory.schema)
//如果两个验证都通过,则调用insert方法
Meteor.call('inventory.insert',insertDoc,(err,res)=>{…})
call('purchaseInventory.insert',insertDoc,(err,res)=>{…})
}
})
注意,如果您需要在服务器端成功地进行两次插入,那么应该编写第三个Meteor方法,在一次方法调用中在两个集合中显式插入单个文档。如果您的Mongo版本>=4,则可以将其与


    +autoForm(class="inventoryForm" schema=schema  id="inventoryInsertForm" validation="blur" type="method" meteormethod="inventory.insert")
        .formGroup
          +afQuickField(name="name" label="Name")
          +afQuickField(name="manufacturer" label="Manufacturer")
          +afQuickField(name="description" label="Description")
          button#invenSub(type="submit") Submit