How to grab nested meteor user profile and store it in the collection?


#1

Hello,

I am having difficulty grab and store in the collection the current users nested user profile.

How do you grab this - insurer:

"profile" : {
        "name" : "User Name",
        "company" : "KRkwBeicgL5ayedGa",
        "branch" : "s9NYgXqsgSigRuJWR",
        "insurer" : [ 
            "sE5pHS6Cgy66wminx"
        ]
    }

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.

What should be the proper code for this?


#2

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.


#3

Hi @shock,

Thank you for the response!

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?

This is not working: user.profile.insurer

Update: this is the userprofile look like:

"profile" : {
        "name" : "User Name",
        "company" : "KRkwBeicgL5ayedGa",
        "branch" : "s9NYgXqsgSigRuJWR",
        "insurer" : [ 
            "sE5pHS6Cgy66wminx"
        ]
    }

Thanks.


#4

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


#5

What if i want to grab all value?


#6

define how else you want to “grab” them, or what you mean by grab ?
it is array of them


#7

To store in the collection.

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.


#8

https://docs.mongodb.org/manual/reference/operator/update/push/#example-push-each


#9

Thanks @shock

I will try this and will let you know if it works.


#10

but when I am thinking about autovalue, it would probably expect something like

arrayProperty.0 = firstItem
arrayProperty.1 = secondItem

or something like that as an input

but that depends on your schema for that array field


#11

Yeah, something like that.

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.

Thanks a lot @shock for the time! :smile:

I will update this thread once i solved my issue.