Document field: "entry.friends.status" dont work

I cant find information on how to console log a document field (array) name

console.log(entry.friends.status); <- Dont work

      var thefriends = Friends.find({ _id: Meteor.userId() }).fetch();
      console.log(thefriends.friends);

      let myCount = 0

      let inviteCount;
      thefriends.forEach(function(entry) {
        
      console.log(entry.friends[status]);
      
      if (entry.friends.status == 2) {
        myCount +1
      }
 
    });

    console.log(myCount);

DB: friends

{
    "_id" : "8YBhc3AeKbfHaoNJ9",
    "friends" : [ 
        {
            "id1" : "Nt4ZTwqAqKfNo3avi",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }, 
        {
            "id1" : "sQEAn5HiCtEnWXHPs",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 1
        }, 
        {
            "id1" : "FXsCRBoPveGN6XvbJ",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }, 
        {
            "id1" : "3D5sGHGSWHrmTKc4t",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }
    ]
}

.fetch() returns an Array (of Documents), and you try a console.log() on a property (‘friends’) of this Array. Don’t you get any error by doing this ?

If you want to get only one Document (as your query is based on _id) you can use .findOne() instead of .find(). (this way you directly get the Mongo Document)

.

You try to access an index/property of friends by using a variable called status : you don’t have any such variable.

.

This is wrong, you can’t increment your variable this way. I think you mean :

myCount = myCount + 1;
// or
myCount += 1;
// or 
myCount++;
// or
++myCount;

.

You do realise that, with your .forEach(), you loop over 1 item ? Which is the Document you get from your query (based on the current user ID), and not over the property ‘friends’ of this Document.

I have tried a lot and cant find the solution on how to count where status = 2

borntodev do you how to count the status when in a array in a document? i’m lost :frowning:

cant find info on how to access status

@thorus, no offence but, I think you should learn more about Javascript. Here we have a really basic operation to do.

If I reuse what you tried :

const friendDocument = Friends.findOne({ _id: Meteor.userId() });
const friendsArray = friendDocument.friends;
let myCount = 0;

friendsArray.forEach(function ( friend ) {
  // will output something like { id1: 'xxx', id2: 'yyy', status: Z }
  console.log( friend );

  if ( friend.status === 2 ) {
    ++myCount;
  }
});

console.log( myCount );
1 Like

Yes I know :frowning: I have to find some time to learn js the basic, but got a customer I could not say no to.

Now I get this error:

hook: "TypeError: Cannot read property 'friends' of undefined"

found in

It means your database cannot find any Document with such ID.

You will have to check how data are inserted in your database.

This is direct from the mongodb

That’s because you’re trying to index an Array’s status property, e.g.: ([].status) will always be undefined

{
    "_id" : "8YBhc3AeKbfHaoNJ9",
    "friends" : [ 
        {
            "id1" : "Nt4ZTwqAqKfNo3avi",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }, 
        {
            "id1" : "sQEAn5HiCtEnWXHPs",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 1
        }, 
        {
            "id1" : "FXsCRBoPveGN6XvbJ",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }, 
        {
            "id1" : "3D5sGHGSWHrmTKc4t",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        }
    ]
}

console.log(entry.friends) Will yield the following:

[{
            "id1" : "Nt4ZTwqAqKfNo3avi",
            "id2" : "8YBhc3AeKbfHaoNJ9",
            "status" : 2
        },...]

Notice it’s an array, arrays don’t have the .status property.

So, based on what you want

Fetching [2, 1, 2, 2] which are each status:

console.log(entry.friends.map(friend => friend.status)) will result on an array of numbers, as you’re using map to extract it’s internals as simple array

Counting the amount of friends that have status === 2:

      var thefriends = Friends.find({ _id: Meteor.userId() }).fetch();
      console.log(thefriends.friends);

      let myCount = 0

      let inviteCount;
      thefriends.forEach(function(entry) {

           // Solution 1, using map and filter
           myCount += entry.friends.map(friends => friends.status).filter(status => status === 2).length 
      })

Other solutions:

let statusSearched = 2;
// statusSearched could be your function argument
var thefriends = Friends.find({ _id: Meteor.userId() }).fetch();
/*
 Remember .status is a property of friends, not of an array, that's why we use map
 casue [].status doesn't exist, but arr = [{status: 2}] can be obtained with arr[0].status
where map iterates in the way of arr.map(friend ...) is equivalent to arr[i++] === friend
*/
let allStatusFriends = thefriends.map(thefriend => thefriend.friends).map(friend => friend.status).filter(status => status === statusSearched); // where statusSearched was defined previously, or just use "2"
console.log('Friends with status 2', allStatusFriends.length);
1 Like

PERFECT! big thanks.

I will use:

var thefriends = Friends.find({ _id: Meteor.userId() }).fetch();
      console.log(thefriends.friends);

      let myCount = 0

      let inviteCount;
      thefriends.forEach(function(entry) {

           // Solution 1, using map and filter
           myCount += entry.friends.map(friends => friends.status).filter(status => status === 2).length 
      })
1 Like