mongodb - 使用一个 autoForm,我需要将数据插入到两个集合中
问题描述
我目前正在开发一个库存系统,该系统将零件集合和采购集合作为应用程序的骨干。每个零件多有相应的采购。即一个零件必须有一个与之关联的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"
//purchases
PurchaseInventory.addLinks
part:
type:'one'
collection:Inventory
field:'partId'
index: true
最后是 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
重申一下我的目标是让每个项目都具有指向其相应购买数据的链接。
解决方案
最直接的方法是使用autoform 表单类型normal
并为提交事件创建自定义事件处理程序(或者您可以使用 AutoForm 挂钩onSubmit
)。从那里您可以使用AutoForm.getFormValues
API 函数来获取当前文档。
因为我不喜欢 Coffeescript,所以我会提供以下 Blaze/JS 代码,但我认为它应该给你这个想法:
{{# autoForm type="normal" class="class="inventoryForm" schema=schema id="inventoryInsertForm" validation="blur"" schema=schema id="insertForm" validation="blur" }}
<!-- your fields -->
{{/autoForm}}
/**
* validates a form against a given schema and returns the
* related document including all form data.
* See: https://github.com/aldeed/meteor-autoform#sticky-validation-errors
**/
export const formIsValid = function formIsValid (formId, schema) {
const { insertDoc } = AutoForm.getFormValues(formId)
// create validation context
const context = schema.newContext()
context.validate(insertDoc, options)
// get possible validation errors
// and attach them directly to the form
const errors = context.validationErrors()
if (errors && errors.length > 0) {
errors.forEach(err => AutoForm.addStickyValidationError(formId, err.key, err.type, err.value))
return null
} else {
return insertDoc
}
}
Template.yourFormTempalte.events({
'submit #insertForm' (event) {
event.preventDefault() // important to prevent from reloading the page!
// validate aginst both schemas to raise validation
// errors for both instead of only one of them
const insertDoc = formIsValid('insertForm', PurchaseInventory.schema) && formIsValid('insertForm', Inventory.schema)
// call insert method if both validations passed
Meteor.call('inventory.insert', insertDoc, (err, res) => { ... })
Meteor.call('purchaseInventory.insert', insertDoc, (err, res) => { ... })
}
})
请注意,如果您需要两个插入都在服务器端成功,您应该编写第三个 Meteor 方法,该方法在一个方法调用中显式地在两个集合中插入单个文档。如果您的 Mongo 版本 >= 4,则可以将其与transactions结合使用。
推荐阅读
- sql - GBQ中的窗口函数按顺序排序行为规范
- javascript - reactjs中返回未定义值的函数
- sql - 将 JSON 函数与 PostgreSQL 子查询一起使用
- reactjs - 在 react native 中,当切换屏幕时,默认屏幕也会被执行
- python - TypeError: __array__() 接受 1 个位置参数,但给出了 2 个(图像分类 Keras)
- optimization - 优化求解器在不同的编程语言中会改变其性能?
- mysql - Sequelize where 条件不查询正确的 DB 列
- javascript - 生成器的过滤器和映射函数
- python - 如何使我的 point_in_polygon 函数更 Pythonic?
- assembly - 6502 通过 TXA 和 TAX 使用 CMP BNE 循环