thorus
September 1, 2017, 9:40am
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, } });
thorus
September 3, 2017, 5:40pm
2
Need more info?, I still need help
mrzafod
September 5, 2017, 3:19am
3
Pseudocode
const categories = Categories.find(...).fetch();
return _.filter(categories, ({_id}) => Tasks.find({categoryId: _id}).count());
2 Likes
thorus
September 5, 2017, 12:31pm
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?
thorus
September 6, 2017, 10:39am
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}));
Did you intend to use checked: false
or checked: 0
?
thorus
September 6, 2017, 10:44am
7
in the Db it is true og false
Then you should use checked: false
.
thorus
September 6, 2017, 10:47am
9
it do not work when I write checked: false. it have noget effect on the output
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.
thorus
September 6, 2017, 10:53am
12
I have tried with “” and without, with the same result
Can we see some sample documents?
thorus
September 6, 2017, 10:56am
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")
}
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.
thorus
September 6, 2017, 11:28am
16
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
Thanks to @mrzafod as well