Lenght or count on a document field array


#1

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”
]


#2

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


#3

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


#4

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

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


#5

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


#6

My brain have just collapsed :slight_smile:

This dont work

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


#7

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:


#8

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


#9

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?


#10

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?


#11
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


#12

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

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


#13

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.


#14

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.


#15

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


#16

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