I am currently working on a project that involves displaying and filtering data from a MongoDB collection based on certain criteria.
However, I encountered an issue where even after applying a filter using a pipe, all the data is displayed if at least one entry meets the condition. I specifically want only the users who meet the conditions to be shown. I suspect it might be a logic error, as I have tried various solutions but haven't been able to find a resolution.
The structure of my MongoDB data looks something like this (example provided):
{
"_id": {
"$oid": "590b8bfb51123bed5e4b6dda"
},
"city": "Madrid",
"type": "admin",
"username": "admin",
"password": "admin",
"email": "admin",
"phone": 123,
// More fields...
"__v": 0
}
My current pipe implementation seems to be causing the problem (I believe the issue lies in the filter function). Essentially, the filter method iterates over each user object, retrieves the value of 'user.knowledgeLevel.media.java,' compares it to the 'javalevel' value specified in the HTML, and returns the user if it meets the condition:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'javafilter'
})
export class JavaFilter implements PipeTransform {
transform(users: any, javalevel: any): any{
if(javalevel === undefined) return users;
return users.filter(function(user){
for(let user of users){
for(let kn of user.knowledgeLevel){
if(kn.category==='media'){
if(kn.subcategories.java>=javalevel){
console.log(user);
return user;
}
}
}
}
})
}
}
Below is a snippet of the table which will be generated:
<tr *ngFor="let user of users | javafilter:javalevel" >
<td id="center" class="col-md-4">{{user.email}}</td>
<td id="center" class="col-md-3">{{user.phone}}</td>
</tr>
The 'javalevel' value referenced above can be found within the same HTML file, structured like this:
<div id="java">
<div class="col-md-10"><h5>Java</h5></div>
<div class="col-md-2"><h5>{{javalevel}}</h5></div>
<input type="range" min="0" max="20" [(ngModel)]="javalevel" step="1" />
</div>
Your assistance with resolving this issue would be greatly appreciated. Thank you!