Hey guys.
Right now I’m working on an e-commerce app, I use Meteor 1.3.
For now I’m stucked on defining relations between two collections. I have two of them, let’s say Products and Customers, I use meteor-collection-helpers to manage this task and for now my code looks like this:
Products.js
import {Mongo} from ‘meteor/mongo’;
import {Customers} from ‘./customers.js’;
And this is the method to insert a new customer into Customers collection: Customers.insert({ name, phone, email, productId: Products.findOne().getId(), product: Products.findOne().getName(), createdAt: new Date(), });
RIght after order form is submitted I get a new document but the problem is I can correctly apply insert method for only first document, whe I try submitting order form for the second order this document still grabs the name and id of the first document, so for now it makes no sense to use this form
I actually changed the code a lot while googling but ended up with nothing and saved pieces I showed you because at least it works even for the only first product.
I guess I did something wrong while defining productId method but can’t get it, so I hope to find help here. Any suggestions will be warmly welcomed and if I will find out my mistake before any posts in this topic I 'd definetely share it.
Hey, sorry for late response, didn’t have an opportunity to work on my problem.
Yes, I understand that I should somehow pass product’s id, but I just don’t really know how to do it and it got me stucked
For example, as you can see I use a helper to retrieve id but it doesn’t work, also I tried to return another value, like const productName = Products.find({pid: product._id}) but it makes no difference
Alright, finally I found a solution. As @bandit said, I didn’t specified which ID I need to grab so insert grabs only the first document in collection. By the way I did it without any additional package, the final query looks like this: productName: Products.findOne(this.props.product._id).productName
Hey guys, another quick update for anyone wondering how to make such relations possible through methods. As you can see at first I solved this issue by passing the right ID into productName query when inserting a document, insecure package was active though. When it comes to Meteor methods the final solution looks like this: Customers.js
… declaring collection and methods