So is everything looking good? And is it possible to show a counter of how many followers are following a specific user? I kind of need this up and running soon for my latest website update. Thanks for the help!
Hey Pawtalog!
I’m glad to hear you got it working
Just like with the follow button, there’s no out-of-the-box way of counting how many followers one has. What you can do, is for each follower, count how many records he has in the “Followings” Collection where his userId is under the “followee” column.
One way of doing this, is adding a text component to the repeating template, and setting it’s value to the number of results matching the query I described above. You can read more about working with repeaters here .
You should probably start with something like this:
import wixData from "wix-data";
$w.onReady(function () {
// tell the repeater you want to update each item when it's ready
$w("#yourRepeaterNickname").onItemReady(function($w, itemData, index) {
// get the userId of the item (i.e. the followee)
const followeeId = itemData._owner;
// find all the records where followee === followeeId
wixData
.query("Following")
.eq("followee", followeeId)
.find()
.then(function (results) {
// assign the number of resultsto your "counter" text elemnt
$w("followerCounterTextInRepeatedLayout").text =
results.length
});
});
});
This is just a general structure of what it should look like. You’ll have to match the names, check that it works and fix whatever doesn’t, but this should definitely lead you in the right direction.
Good luck!
J.
OK, I did everything but I don’t know how to find the value. Here is what I’ve done:
import wixData from “wix-data”;
$w.onReady(function () {
// tell the repeater you want to update each item when it’s ready
$w(“#repeater1”).onItemReady(function($w, itemData,) {
// get the userId of the item (i.e. the followee)
const followeeId = itemData._owner;
// find all the records where followee === followeeId
wixData
.query("Following")
.eq("followee", followeeId)
.find()
.then(function (results) {
// assign the number of results to your "counter" text elemnt
$w("#text56").text = 100;
results.length;
});
});
});
I still don’t know how to find the value. How can I do this?
Hey Pawtalog!
What is the value that you don’t know how to find?
I’m having trouble finding the value of the text element. I’m not clear as to what I have to do.
Do you mean what value to assign to your “counter”?
If that’s what you’re missing, notice what you did in the code:
wixData
.query("Following"
.eq("followee", followeeId)
.find()
.then(function (results) {
// Here, results is all the records that have "followee"
// equal to followeeId. The amount of records is the number
// you're looking for
$w("#text56").text = results.totalCount;
});
You can see more about what is available on the results object here
OK,
I finished what I can do with my primitive knowledge of coding. Sorry about all of the comments
However, I do believe that I need things edited because I’m sure that somewhere I have made a mistake. Here is a link to the editor (page is called pet profiles ):
Thanks for the amazing support so far!
-Pawtalog
If for whatever reason the link isn’t working, just tell me and i’ll post it again
Hello. I am trying to also create a following function. I am able to get it the id of the follower but not the followee. I am using your code exactly. Is it possible that it is the permissions of the database or how the field should be managed? I tried doing everything step by step and it’s not giving me the followee’s id.
How does the program know which user you are trying to follow? How do I set that up?
@silvergoyal I am having the same issue. Did you find a resolution?
Hello. I am trying to also create a following function. I am able to get the id of the follower but not the followee. I am using your code exactly. Is it possible that it is the permissions of the database or how the field should be managed? I tried doing everything step by step and it’s not giving me the followee’s id. Any recommendations would be greatly appreciated
Hi, I am pretty new to website building and have never used Wix Code and I was wondering if someone would be able to help me with this. I want to create a follow button along with a counter of how many followers people have. I have read the above comments on this but got stuck because I am currently using a custom form with multiple collections.
My question is, do you have to connect the follow button to a database in the custom form or do you have to connect it on the dynamic page? Any and all help will be appreciated. I am assuming this is the case and will try the methods used above when I can. Thank you for reading this.