I’m using collection2 autovalue to grab user profile, I can grab by company like this:
autoValue: function() {
if (this.isInsert) {
var user = Meteor.users.findOne({ "_id": this.userId }, { fields: { "profile": 1 } });
if (user) {
return user && user.profile.company;
}
} else {
this.unset(); // Prevent user from supplying their own value
}
}
same as for branch and name but for the insurer it didn’t work like this:
return user && user.profile.insurer;
before, the insurer key has only one value, the code above works but when i nested it to have array of values, it didn’t work.
I dont understand what you mean by filter.
In the example I cant see any filter, only check if field exist and that you can do with array as insurer.
Or check the array lenght if it is not 0 like && user.profile.insurer.length
PS: and in your place, I would not put branch/company and such to profile array, which can every user change by themselves.
Apologize for the confusion, what I mean by filter is, every user has company, branch and insurer, this is my approach on filtering the view and documents that the user created.
Every time the user created a document it will grab the users profile.
for branch i use
return user && user.profile.branch;
for company i use:
return user && user.profile.company
for Insurer:
return user && user.profile.insurer
In short, every document has a company, branch and insurer field. As well as, every user has a company, branch and insurer.
Don’t worry, i disabled the editing of profile by the user if not admin. They have to contact admin if they want to change their profile.
So my problem is, since insurer is an array, how do i grab it?
You can grab it by user.profile.insurer
but if you want return 1st value, you would need user.profile.insurer[0]
or use some standard javascript function to check if array have some value in it
with for example http://www.w3schools.com/jsref/jsref_indexof_array.asp
The reason why insurer has many is company has a possibility of multiple insurer. If the insurer is logged in the system, it will display all the documents of the company that he insured.
The reason why i use autovalue is to add in the schema under the hood, meaning, the user doesn’t know that his user profile is added to the documents. So, if the user submitted the document, it will add his company, branch and insurer on the documents.
I’ve had success in grabbing it if it’s not an array. But the moment when my client contacted me that a company has a possibility of multiple insurer, i tested it but to no avail.