If no tasks in category then dont show task category


#1

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,  } });

#2

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


#3

Pseudocode

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

#4

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?


#5

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}));

#6

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


#7

in the Db it is true og false


#8

Then you should use checked: false.


#9

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


#10

parentId: _id works fine


#11

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.


#12

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


#13

Can we see some sample documents?


#14

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")
}

#15

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.


#16

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


#17

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());

#18

It works :slight_smile:

Thanks


#19

Thanks to @mrzafod as well :slight_smile: