I try to filter a dataset to only show content uploaded by some specific people.
Current user for example is “sakisspahos” .
There is the " Friends " Database, and i want to filter a dataset “Videos” (connected to a database “Videos”) by a field " Uplaoder " to show only items where " Uploader " is one of the users on the " Friends " Database.
So based on the example, friends with sakisspahos , are " konstantinapourl " and " kostasmpounatso " so only videos uploaded by them should be shown.
Friends Database
Videos Database
Code used to filter the repeater
-Items on line 37 are returned correctly.The 2 friends items.
-Line 56 works(the one that uses .not curruser), so the repeater doesn’t show videos uploaded from “sakisspahos”.
-The issue is, that it displays items it shouldnt. By users that are not on the or conditions.
Code(pasted)
function getfriends() {
let curruser = $w("#dataset3").getCurrentItem()._id //Gets the current user, in our example "sakisspahos"(his ID)
let filter = wixData.filter();
wixData.query("Friends")//Queries the "Friends Database" to get all the Users that are friends with "sakisspahos",based on the ID
.eq("approved", true)
.eq("user1", curruser)
.or(
wixData.query("Friends")
.eq("approved", true)
.eq("user2", curruser)
)
.find()
.then((results) => {
//Here, the items are returned correctly, i checked by console.loging the results.
//So the results are "konstantinapourl" and "kostasmpounatso" items.
console.log(results.items)
if (results.items.length > 0) {
for (var i = 0; i < results.items.length; i++) { //For each user found do
if (curruser === results.items[i].user1) {
//If the current user is on the user1 field, this means that the user2 field contains the friend ID.
filter = filter.or(
wixData.filter("Videos")
.eq("uploader", results.items[i].user2)//an or for the uploader to be the user2(the friend)
)
} else {//If curruser is not the user1, this means he is the user2, so the friend is the user1.
filter = filter.or(
wixData.filter("Videos")
.eq("uploader", results.items[i].user1)//an or for the uploader to be the user1(the friend)
)
}
}
filter = filter.not(//filtering not for the uploader to not be sakisspahos(the current user).
wixData.filter("Videos")
.eq("uploader", curruser)
)
$w("#dataset1").setFilter(filter);
console.log(filter)
} else {
// User has no friends.
}
})
}