Helper count always 0

Hi,
I am trying to bilud a helper which checks whether three fields were filled:
//helper
contactStatus: function(){
var status = Contacts.find({_id: this._id, contacts_support: {email: {$exists: true}, phone: {$exists: true}, name: {$exists: true}}});
return status.count() // 0 always

}

The problem is that it returns 0 even if the fields are filled. (so i thought it would return 1.)
What’s wrong?
thanks.

Please add a line:

console.log("any contacts: " + JSON.stringify(Contacts.find().fetch()));
console.log("status: " + JSON.stringify(status.fetch()));

before return and check if there’s any data found at all.

Thanks,
The first line returns the expected long json, including the specific data at _id: ‘CoriqrThcSL72M5oR’.
“contacts_support”:{“email”:“avi@dddd.co.il”,“phone”:“0543332940”,“name”:“avi”}

for the second line I entered to the console though, I get:
status: []
And this line :
console.log("any contacts: " + JSON.stringify(Restaurants.find({_id: ‘CoriqrThcSL72M5oR’, contacts_support: {email: {$exists: true}, phone: {$exists: true}, name: {$exists: true}}}).fetch()));
Also I get:
any contacts: []

It looks like your data structure looks like

contacts_support: {
  email: <...>
  phone: <...>
}

If so you should filter data by full field name:
{"contacts_support.email": {$exists: true}}

1 Like

I tried that, now I always get count 1.

var status = Restaurants.find({_id: this._id, “contacts_support.email”: {$exists: true}, “contacts_support.name”: {$exists: true}, “contacts_support.phone”: {$exists: true}});
return status.count() //1
it returns the status object even if those fields do not exist.

You’re passing in a specific restaurant ID to match on (in your example I’m assuming you mean _id instead of id). If a match is found you’re only ever going to get back one restaurant, since Mongo ID’s are unique.

thanks, right but this is clear. The question is why I’m getting one, while those fields are empty. e.g 0 is expected.

The fields are empty or they don’t exist? This makes a difference when using $exists. From the Mongo docs:

When is true, $exists matches the documents that contain the field, including documents where the field value is null. If is false, the query returns only the documents that do not contain the field.

1 Like

Seems like I’ve missed the Null part on the doc. :frowning:
Great, this works:
“contacts_support.email”: {$ne: “”}
Thanks a lot for pointing that out…!.