现在我正在开发一个电子商务应用程序,我使用Meteor 1.3。现在我正忙于定义两个集合之间的关系。我有两个,比如说Products
和Customers
,我使用流星收集助手来管理这个任务,现在我的代码看起来是这样的:
Products.js
import {Mongo} from 'meteor/mongo';
import {Customers} from './customers.js';
export const Products = new Mongo.Collection('products');
Products.helpers({
getName() {
return this.productName;
},
getId() {
return this._id;
},
customers() {
return Customers.findOne(this.customerId);
}
});
客户.js
import {Mongo} from 'meteor/mongo';
import {Products} from './products.js';
export const Customers = new Mongo.Collection('customers');
Customers.helpers({
tours() {
return Products.find({customerId: this._id});
}
});
这就是将新客户插入Customers
集合的方法:
Customers.insert({
name,
phone,
email,
productId: Products.findOne().getId(),
product: Products.findOne().getName(),
createdAt: new Date(),
});
在提交订单后,我立即得到了一个新的文档,但问题是我只能正确地对第一个文档应用插入方法,当我尝试为第二个订单提交订单时,这个文档仍然会抓取第一个文档的名称和id,所以目前使用这个表单没有意义。事实上,我在谷歌搜索时更改了很多代码,但最终一无所获,并保存了我向您展示的部分,因为至少它对唯一的第一个产品也有效。我想我在定义productId
方法时做错了什么,但无法得到它,所以我希望在这里找到帮助。任何建议都将受到热烈欢迎,如果我能在这个主题的任何帖子之前发现我的错误,我会明确地分享它。
UPD
解决方案是尽可能简单,我不需要我上面写的包。正确的查询是成功的product: Products.findOne(this.props.product._id).productName
。
基本上您自己发现了问题,但不知道解决方案。Products.findOne()
总是会返回第一个文档,除非您传递像Products.findOne({_id :'id'})
这样的参数。