Lenght or count on a document field array

Hi

Howto get the count of a array

This do not work

 countwaitingapprovalevent() {

            return Meteor.users.findOne({_id: Meteor.userId()}).pendingId.length;

          }

Need the lenght/count of pendingId

“pendingId” : [
“TTHyxmmpdqzpmGttc”,
“FjoBeedAQvJYSxgAs”
]

What are you getting now? Are you sure you are only querying this after the user document is available? The user doc is not immediately available on startup. You can also save yourself some typing by using Meteor.user() instead of findOne

1 Like

Also, you need do explicitly publish the extra pendingId field, it is not automatically published.

1 Like

I just need a simple way to count how many objects there is in the pendingId array.

“pendingId” : [
“TTHyxmmpdqzpmGttc”,
“FjoBeedAQvJYSxgAs”
]

yeah… length does work, but you need the user document with the pendingId field published. So did you do the steps I mentioned above?

1 Like

My brain have just collapsed :slight_smile:

This dont work

var countEvent = Meteor.users.findOne({_id: Meteor.userId()});
return countEvent.pendingId.length

By “not work”, do you mean it crashes, or you get undefined?

As I said, the “pendingId” fied is not in the user document automatically. You need to publish it.
You need to read this, it will tell you how to do that:

2 Likes

I did it like this:

          countwaitingapprovalevent() {
            let myCountEvent = 0

            Meteor.users.find({ _id: Meteor.userId() }).fetch().forEach(function(doc) {
                myCountEvent = doc.pendingId.length;
               
          
            })

            return myCountEvent;

          }

Do you think it is okay? or is it a bad way to do it?

I will use it in both my Blaze and Vue apps

Yes… it’s pretty bad.

I’m not sure you are reading what I’m saying here.

Have you published the pendingId field?
Are you doing this query before the user model is available?
Have you looked in your console to see if your program is crashing?

I still have:

autopublish
insecure

I will publish it all later, the code I wrote work now, but you say there is a better way to do it?

const user = Meteor.user();

// This is basically "if user exists, then get the number of pendingId,
// otherwise return 0 because the user has not been loaded yet. 
const count = user ? user.pendingId.length : 0;

Try to do a console.log(Meteor.user()) also to see if the user is actually logged in and loaded

I get userid if I use this: console.log(Meteor.userId())

This dont give me anything console.log(Meteor.user())

That means you are rendering before the user document has reached the client. You can’t just use minimongo in React components, because it takes time to get data from the server to the client. You need to use WithTracker and pass down the results of database queries to your display components.

1 Like

The best way would be to use withTracker as as suggested by @jorgeer it would be important to check first if the user object isn’t undefined which happens to be so if the subscription isn’t ready yet.

I Dont know react, I use meteor with Vue and Blaze

1 Like

It is the same, The reason why @jorgeer kept emphasizing on the publish is because getting the length on the client will be affected with how you have published, if you haven’t published and you don’t have auto-publish then you will not be able to get data on the client, make sure your publish the clientId and as I mentioned in my earlier reply check if the object isn’t undefined first.

const user  = Meteor.users.findOne({_id: Meteor.userId()});
const pendingId = user ? user.pendingId : null;
console.log(pendingId.length)

Double your schema to check if that pendingId field is there.

All the best

1 Like