I am trying to filter the following dataset so that the logged in user can see all the users that have liked them in a repeater.
Here is the code I have so far that is not working:
$w.onReady( async function () {
const results = await wixData.query("Liked").eq('liked', user.id).find();
$w("#repeater3").data = results.items;
} );
I attempted it this way as well and it did not work either:
//$w.onReady(async function () {
// $w("#dataset1").onReady(() => {
// $w("#dataset1").setFilter(wixData.filter()
// .eq('liked', user.id));
// })
//});
Essentially, I want the currently logged in user that was liked to be able to see the information of the people that liked them. I am not sure if a reference field can be filtered the way I am trying to filter it.
.eq('liked', user.id)
Since ‘liked’ is a reference field, is eq the correct function to use? How do I filter the column to get the information I need?
I think you will have to go to the reference which the referenceField is connected to. It is just a referenCED-FIELD which takes you to another DATABASE and to another REFERENCE-FIELD.
Where does it link to / goes to ? Which FIELD (FIELD-ID) ? to “user.id” ??? Not to “title” for example?
The Liked Database has two reference fields, ‘userthatliked’ and ‘liked’. When the user clicks the like button, their action of liking is inserted into the Liked database. The reference fields both get their information from a database called userProfile that has all the information of all users.
The user.id is the id of the currently logged in user, taken from this:
let user = wixUsers.currentUser;
The user.id is one column in the userProfile database, which always is inserted into both reference fields in the Liked database. That is why I am using it to filter the reference column. I’m not sure that eq would work here though.
Here’s what the insert statement looks like:
$w.onReady(function () {
$w('#like').onClick(async (event, $w) => {
wixData.query("userProfile")
.eq("_owner", user.id)
.find()
.then((results) => {
let Item = results.items[0];
let toInsert = {
"liked": $w('#dataset1').getCurrentItem()._id,
"userthatliked": Item
};
wixData.insert('Liked', toInsert);
})
});
});
Well unfortunately my issue is still there. I am using eq and it doesn’t work. This page is essential to my site.
Well i would suggest you to log every code-step, to find the problem.
$w.onReady(function () { console.log("START")
$w('#like').onClick(async (event, $w) => { console.log("CLICK")
console.log("USER-ID = " + user.id)
wixData.query("userProfile")
.eq("_owner", user.id)
.find()
.then((results) => { console.log(results)
let Items = results.items;
console.log(item)
let firstItem = results.items[0];
console.log(firstItem)
console.log(firstItem.title)
let toInsert = {
"liked": $w('#dataset1').getCurrentItem()._id,
"userthatliked": Item
};
wixData.insert('Liked', toInsert);
})
});
});
Take a look at your console-log-results and check if they match to what you need.