I have queried a dataset on the backend (the reason is I needed to bypass permissions as my dataset is linked to PrivateMembersData). I store this result into a string called “originalRows” which is accessible at the top of the page for all methods (set it above onReady).
I then want to filter by a dropdown and using console I have found that the issue is in my if-statement of the filterResults() method. My code displays the original query on the repeater perfectly. I want to filter by “Position”. Here is my code:
function filterResults (){
let pos = $w ( ‘#positionDropdown’ ). value ;
console . log ( originalRows );
console . log ( “pos” );
console . log ( pos );
if ( pos && pos !== $w ( ‘#positionDropdown’ ). placeholder ) {
console . log ( “inside if statement” );
filteredRows = originalRows . filter ( item => item . position . _id === pos ); //THIS IS WHERE THE ISSUE LIES - I have even used item.position without "_id", Also not working.
}
console . log ( "res" );
console . log ( filteredRows ); //THIS RETURNS NOTHING
**return** filteredRows ;
}
I have another method that does the “onChange” of the dropdown to execute this above method. Why is the row in the if-statement not working? I am having syntax issues I think.
You are using === so, the left and right hand side have to be the same type. Just double check that or force the types to the same.
Do a quick check with typeof for both POS and your other item you are comparing against.
console.log(typeof undeclaredVariable);
Also make sure you are comparing the same things. For example, if _ID is a long member ID and pos is a numeric value, it will never work.
Start there and see how you go. If not, please provide a little more info from your debug and include the output for POS, typeof, etc.
You need to make sure that originalRows items have the properties you are trying to access. If you can console.log( originalRows ), it would be very helpful to show you and us, what it looks like.
Thanks for the help Kyle. I checked out pos and that is a string.
I am not sure how I can check the typeof item.position as it’s in the brackets. However, position is a referenced field from my Player Dataset. Player dataset Position field references the Positions dataset Title field. And this Positions Title field is what is populating the Dropdown. In turn, I am displaying the Players that match the Position of the dropdown. The Position Title field is a Text (string) field according to my dataset.
I tried using item.position._id because I saw somewhere when there are reference fields, ID seems to be called. But not sure if it applies here.
Currently the console.log(originalRows) output AFTER my if-statement is returning an empty array:
-
length: 0
-
[[Prototype]] : Array(0)
Hi Bruno. In my code above you will see I have consoled originalRows before the if-statement. At that point, all my rows are displaying on console and on the repeater.
It’s exactly after the if-statement that when I console originalRows that it shows an empty array, which makes me think my comparison of the === is wrong.
This is the originalRows after the if-statement:
-
length: 0
-
[[Prototype]] : Array(0)
@kscholiadis I know where the problem is, as you already know, but I need to make sure that the property you are looking for, exists…
As Bruno mentions — comes down we think to comparison. What if you added a hard coded test into the filter, I don’t know - something like 1 === 1 or _id != “” that sort of thing that evaluates to be true all the time. Try and get the filter to succeed with this sort of logic. If it works and you are returned the array back in tact, then you can be rather sure that the problem is in your comparison.
If nothing matches the condition you will get an empty array returned.
I tested the 1 ===1 and it worked as you expected. The array AFTER the if-statement displays all the fields same as BEFORE the if-statement. How would you advise I compare the item.position (reference field) to the pos string value?
I did try item.position.toString() === pos, but that didn’t work. Not sure if the reference field complicates anything.
This is the console.log(originalRows). The position field shows as:
position: {_id: "99371817-2956-4f31-914d-fb32443…
If I expand the position field:
-
position:
-
category: “Attacker”
-
title: “Striker”
-
_createdDate: Tue Apr 27 2021 10:52:56 GMT+0100 (British Summer Time) {}
-
_id: “99371817-2956-4f31-914d-fb32443a473a”
-
_owner: “4407fa34-b3cf-41ac-9f50-793edd16f999”
-
_updatedDate: Tue Apr 27 2021 10:55:20 GMT+010…
@bwprado Please read my reply to Kyle, I hardcoded a successful if-statement and it then displays the originalRows with the following:
The position field shows as:
position: {_id: "99371817-2956-4f31-914d-fb32443…
If I expand the position field:
-
position:
-
category: “Attacker”
-
title: “Striker”
-
_createdDate: Tue Apr 27 2021 10:52:56 GMT+0100 (British Summer Time) {}
-
_id: “99371817-2956-4f31-914d-fb32443a473a”
-
_owner: “4407fa34-b3cf-41ac-9f50-793edd16f999”
-
_updatedDate: Tue Apr 27 2021 10:55:20 GMT+010…
First of all loop through and work out if there is an _id that looks like the value of pos. What is the value of pos before entering the filter?
Try using a double == if you find values that are the same. _id should be of type text.
Try also just using item._id
The value of pos is displaying as the Title text field of Positions so not sure I can compare the Title to the _id. I have populated the dropdown with the Title text field.
As I write this I am thinking, I populated the dropdown using the front end editor and not using code. Not sure if I should use code instead? But then I may need to query this dataset on backend and pull it to front end too and it may complicate the page code.
All comes down to the comparison
No, you can’t compare title to ID as one may say “Cabbage” and the _ID is 99371817-2956-4f31-914d-fb32443a473a. Never the twain will meet.
I finally solved it. I am a little embarrassed how silly this was but you definitely led me to it, so thank you.
I just needed to put:
item.position.title === pos
The filtering works perfectly. Thanks a million for your time!
Bruno was on the money. No probs.
@bwprado getting the console.log(originalRows) displaying led me to the answer. Thanks for the help.
The code that worked:
item.position.title === pos