If no tasks in category then dont show task category

I have a todo app where I have to list categories, but only list categories that have open tasks.

How is that possible?

return Categories.find({ userid: Meteor.userId(), categoryArray: appId, _id: { $not: appId }}, { sort: { category: 1 } });

return Tasks.find({ parentId: appId}, { sort: { checked: 1,dodate : 1,  } });

Need more info?, I still need help :slight_smile:

Pseudocode

const categories = Categories.find(...).fetch();
return _.filter(categories, ({_id}) => Tasks.find({categoryId: _id}).count());
2 Likes

Hi Mrzafod

I have this:


{{#each catlist}}
        {{> catlisttemp}}
    {{/each}}

<template name="catlisttemp">
     <tr class="{{#if checked}}checked{{/if}}">
        <td colspan="5" class="{{#if checked}}checked{{/if}}" style="text-align:left; padding: 12px 20px;">
              <strong>{{category}}</strong>
        </td>
      </tr>
</template>

How to use your code to only show categories with open tasks?

Why do “checked: “false”” have no effect on the return elements?


 const categories = Categories.find( {userid:Meteor.userId(), categoryArray: appId, _id: { $not: appId }}).fetch();
      return _.filter(categories, ({_id}) => Tasks.find({checked: "false",parentId: _id}));

Did you intend to use checked: false or checked: 0?

in the Db it is true og false

Then you should use checked: false.

it do not work when I write checked: false. it have noget effect on the output

parentId: _id works fine

In your example code:

You used checked: "false" - that’s not the same as checked: false. The first is a string comparison, the second is boolean.

I have tried with “” and without, with the same result

Can we see some sample documents?

Categories:

{
    "_id" : "PNR6iDMCAx8bJS6EC",
    "category" : "Mulige kunder",
    "parentId" : "zx3tGbGdjdL5mgs2E",
    "userid" : "2adDHy4sYrPaEjhyD",
    "createdAt" : ISODate("2017-08-14T11:02:43.225Z"),
    "categoryArray" : [ 
        "PNR6iDMCAx8bJS6EC", 
        "zx3tGbGdjdL5mgs2E"
    ],
    "adminArray" : [ 
        "2adDHy4sYrPaEjhyD"
    ]
}

Tasks:

{
    "_id" : "EvJTybSpjDwGjTXYR",
    "text" : "Find gamle plader",
    "checked" : false,
    "userid" : "2adDHy4sYrPaEjhyD",
    "parentId" : "r6DD4dgSdXbFycAas",
    "dodate" : "",
    "createdAt" : ISODate("2017-07-05T09:52:16.332Z")
}

I’ve just checked that your query returns the correct docs when checked: false.

However, I’ve just re-read the whole thread and the problem is with the filter code you’ve used, which will return an array of cursors as it’s written. I don’t think that’s what you want here - the filter method uses a truthy/falsey return to decide whether or not to include docs in the returned array. A cursor is going to be “truthy”, so all docs will always be returned. @mrzafod’s code used a count() method to determine whether or not there are open tasks - and 0 is considered falsey.

What to use instead? i’m lost, so any good ideers are most welcome

Well, I haven’t tested it, but go with @mrzafod’s suggestion:

const categories = Categories.find( {userid:Meteor.userId(), categoryArray: appId, _id: { $not: appId }}).fetch();
return _.filter(categories, ({_id}) => Tasks.find({checked: false,parentId: _id}).count());
1 Like

It works :slight_smile:

Thanks

1 Like

Thanks to @mrzafod as well :slight_smile: