Im a looking to query one field in database and find where my item is ranked at the moment, depending how many points he has according to that field.
export function dynamicDataset_ready() {
let name = $w("#title").text;
wixData.query("POINTS-PERSONAL")
.eq("totalPoints", name)
.limit(1000)
.find()
.then(result => {
let newresults = result.items;
let numIndex = newresults.findIndex(item => item.totalPoints === name)
console.log ("Index is", numIndex)
$w("#text207").text = String(numIndex + 1);
})
// This function was added from the Properties & Events panel. To learn more, visit http://wix.to/UcBnC-4
// Add your code for this event here:
}
Are you sure the data field totalPonits contains the user name? It sounds odd.
Anyway it is not clear why you’re looking for 1000 results and not only 1 (the top one) + if the results contain only records that match the name, what’s the point in running the findIndex.
Your login is somewhat correct, to do so you’ll need to get all the items, then find the index of that person, but the problem is that you’re refining the query to only show the entries that have the entered name as the totalPoints value, which will always return 1 item, therefore the index will always be 0, am I correct?
You should remove the name of the person from the query to get all the items, then find the index of that person among all entries after that.
return wixData.query("POINTS-PERSONAL").limit(100).find().then(async (x) => {
let all_items = [];
all_items.concat(x.items);
while (x.hasNext()) {
const temp = await x.next();
all_items = all_items.concat(temp);
}
/* The above code will ensure all items are here, otherwise the index
of the person might be above 1000, and therefore the index will be -1 */
const index = all_items.findIndex(items => items.totalPoints === name);
return index;
})
Yes, a typo … It should be let instead of const , I believe the reason the index is -1 is because you’re using the wrong field/value combination, as @J. D. said, totalPoints is probably a numeric type, not a string, and therefore, you should use the name of the person as the field type, not the total points.
@ahmadnasriya
but how then i sort which rank person has depending on totalpoints? These points changing every 5mins. i need to somehow write code ”get index depending on how many total points person has”.
shall i query database and then use sort function a - b, b- a and then somehow filter only the person im looking for and the get index?
In simple words im looking to write the code which shows current rank. rank is measured by totalPoints field.
I have no idea to be honest, it’s becoming too complicated for an example, and I’m afraid I won’t be able to provide more assistance on the forum in this regard, don’t overthink it, it’s too hard for us to understand how your site is structured and how the data is processed, so I suggest you hire a developer to complete this task for you.