Taking specific data from LocalCollection

Hello everybody.
Firstly I want to say that I’m freshly new in Meteor and I try my best to understand everything I need and read the documentation, but I have a problem with one thing.

I create invoices app. I have collections of invoices and customers. In final invoice, which I want to prepare, I want to put some data.

  1. My name and address (I use for this {{currentUser}} “method”)
  2. Number of invoice, etc. I have a collection named: Invoices. My server publish that collection and named it “invoices_editss” (stupid name, I know). My final page, where I put that data, see “invoices_editss” as Object
  3. Info about my customer (name, email, address, etc.). I have a collection named: Customers. My server publish that collection too and named it “customerss”. My final page, where i put data, see “customerss” as LocalCollection.Cursor.

When I want to put, for example, my name on it, I use {{currentUser.myName}}.
When I want to put, for example, invoice number on it, I use {{invoices_editss.invoiceNumber}}
I could put my customerId on it or customerName (because I use it in my Invoices collection) but I need an email or an address, which are in Customers collection.
I don’t know how to take the concrete email/address from collection “Customers”, which my page see that as “LocalCollection.Cursor”.
Of course, as I said, I need concrete address, so I think I will need to use there customerId parameter from Invoices, but… here I stand. I don’t know how to bite that problem.

I would be gratefull if you could help me. Of course I could put some code if it could help.
Thanks!

Anyone?
That problem is really frustrating. Probably it’s really easy, but I don’t know how to do that… :frowning:

more code would help

A localCollection can be turned into a list by using .fetch()

> Cats.find()
LocalCollection.cursor()
> Cats.find().fetch()
[{
  name: 'Odin',
  breed: 'Siberian',
  description: 'truly the mightiest of the kitty cats'
}]

Ok. So would you like a code of collections, js or htmls? I use MeteorKitchen, so don’t be afraid of some code you could see here.

Maybe I will put some here for now and later on I will add more on your demand:

My collections look like that:
“Customers”:

 "name":"customers",
                  "fields":[
                    {"name":"name", "title":"Business Name","required":true, "exportable":true},
                    {"name":"contact_name", "title":"Contact Name","required":true, "exportable":true},
                    {"name":"email","title":"Email","type":"email","exportable":true},
                    {"name":"business_address","title":"Business Address","input":"textarea","show_in_dataview":false,"exportable":true},
                    {
                      "name": "currency",
                      "title":"Currency",
                      "required":"required",
                      "input":"select",
                      "lookup_query": {
                        "name":"currency",
                        "collection":"currency",
                        "filter":{}
                      },
                      "lookup_field":"name",
                      "lookup_key":"_id",
                      "show_in_dataview":false,
                      "show_in_read_only_form":false,
                      "exportable":true
                    },
                    {
                      "name":"currencyName",
                      "title":"Currency",
                      "show_in_insert_form": false,
                      "show_in_update_form": false,
                      "exportable": true
                    }
                  ],
                  "owner_field":"ownerId",
                  "roles_allowed_to_read": ["owner"],
                  "roles_allowed_to_update":["owner"],
                  "roles_allowed_to_delete":["owner"]
                },

“Invoices”:

{
              "name":"invoices",
              "fields":[
                {
                "name":"invoiceNumber",
                "title":"Invoice Number",
                "default":"{{nextinvoiceNumber}}",
                "required":true,
                "show_in_read_only_form": false,
                        "show_in_insert_form":true,
                "exportable":true
                },

                {
                  "name":"date_issued",
                  "title":"Issued",
                  "type":"date",
                  "format":"DD-MM-YYYY",
                  "input":"datepicker",
                  "default":"today",
                  "required":true,
                  "show_in_read_only_form": true,
                          "show_in_insert_form":true,
                  "exportable":true
                },

                {
                  "name":"date_due",
                  "title":"Due",
                  "type":"date",
                  "format":"DD-MM-YYYY",
                  "input":"datepicker",
                  "required":true,
                  "show_in_read_only_form": true,
                          "show_in_insert_form":true,
                  "exportable":true
                },
                {
                                "name": "customerId",
                                "title": "Customer",
                                "input": "select",
                                "lookup_query": {
                                    "name": "customers",
                                    "collection": "customers",
                                    "filter": {}
                                },
                                "lookup_field": "name",
                                "lookup_key": "_id",
                                "show_in_dataview":false,
                                  "show_in_insert_form":false,
                                "show_in_read_only_form": false,
                                "exportable": false
                            },
                {
                  "name": "customerName",
                  "title": "Customer",
                  "show_in_insert_form": false,
                  "show_in_update_form": false,
                  "show_in_read_only_form": true,
                  "exportable": true
                },
                {
                  "name": "totalAmount",
                  "title":"Amount",
                  "type":"float",
                  "show_in_insert_form": false,
                  "show_in_read_only_form": true,
                  "show_in_update_form": false,
                  "exportable": true
              },
              ],
              "owner_field": "ownerId",
                      "roles_allowed_to_read": ["owner"],
                      "roles_allowed_to_update": ["owner"],
                      "roles_allowed_to_delete": ["owner"],

            "before_insert_code": "if(!doc.totalAmount) doc.totalAmount = 0;",
            },

On the server side I do:

Meteor.publish("invoice_editss", function(invoiceId) {
    return Invoices.find({_id:invoiceId,ownerId:this.userId}, {transform:function(doc) { var customer = Customers.findOne({_id: doc.customerId }); if(customer)doc.customerName = customer.name; return doc;  }});
});

Meteor.publish("customerss", function() {
    return Customers.find({ownerId:this.userId}, {});
});

Than on the client side I do:

Meteor.subscribe("customerss"),
Meteor.subscribe("invoice_editss", this.params.invoiceId)

and that:

customerss: Customers.find({}, {}),
invoice_editss: Invoices.findOne({_id:this.params.invoiceId}, {transform:function(doc) { var customer = Customers.findOne({_id: doc.customerId }); if(customer)doc.customerName = customer.name; return doc;  }}),

And on my site “invoice_editss” is seen as Object and “customerss” as LocalCollection.cursor.
I would like to put some data from those two into my client-html page.
I do sth like that:

{{invoice_editss.customerName}}

And that code gives me the name of actual customer.
But I want take an email or address of that customer.

@corvid Should I put it in js or html?

Should I put it in js or html?
Cause I want to take a data from conrecet customer id.

Then make a helper.

Template.myTemplate.helpers({
  cat: function (id) {
    return Cats.findOne(id);
  }
});

Ok. Maybe to be more precised.
I have an invoice. It has invoice number, amount, blah blah blah, and a customerName and customerId.

I have a collection of Customers. It has the same “id” of customer than is on my invoice. But I don’t want to take all data from Customers, only, for example, an email address. Will it works?

– EDIT –

OK, I did my helper:

"customerinfo": function(customerId){
    return Customers.findOne(customerId);
};

So how should I use it in html?
{{# xxxxxx }}?

{{> customerinfo }}

I have problems with that. My console in Chrome send me: “Error: Expected template or null, found: undefined”

In my “app.js” I have:

Template.InvoicesPdfDetails.helpers({
  "customerinfo": function(customerId){
    return Customers.findOne(customerId);
  }
});

And in my “app.html” I have:

<template name="InvoicesPdfDetails">
    <div>
      <p>{{currentUser.profile.address}}</p>
      <p>{{currentUser.profile.email}}</p>
      {{> customerinfo}}
    </div>
</template>

That’s probably because I didn’t give an argument…
I have that argument in “invoice_editss”, so It shoul looks like that? :

{{> customerinfo invoice_editss.customerId}}

Hm? Any idea? I couldn’t get further :frowning:

In principle, you would use your helper like this:

<template name="InvoicesPdfDetails">
    <div>
      <p>{{currentUser.profile.address}}</p>
      <p>{{currentUser.profile.email}}</p>
      {{customerinfo}}
    </div>
</template>

Note, there is no ">" - this is used to embed a template within another template.

You are able to pass data into your helper - you should read the “Helper Arguments” section in the spacebars readme to understand what’s going on when you do this. As far as using {{customerinfo invoice_editss.customerId}}, that is only going to work if invoice_editss is in the Template.InvoicesPdfDetails scope, or is a global object.

A big, big thanks to you! Of course I will read that link, thank you a lot! :smile: