Hi all - I’m having a strange problem when working with my repeaters. I’m creating a page which populates repeaters based on data that is filtered such that the data returned is only data where the current user is the owner (for context, it’s a journal functionality, so I only want the users to be able to see their own journal entries and not others’).
I’m not sure why, but when I load the page, before the $w.onReady() even executes , the repeaters are populated with journal entries for all users first. After the WixDataQuery promise is resolved, this updates and the correct filtering is applied. Any ideas as to why this is happening?
Something important to note is that when I set a breakpoint at the ‘hello world…’ console log, the repeaters are already incorrectly populated. So is it possible that the problem lives outside of my code? I don’t have any dataset attached to the repeaters in the wix editor either.
Thanks so much for any help!
My code:
import wixData from 'wix-data';
import { currentMember } from 'wix-members';
// data is already incorrectly populated here
console.log('hello world....');
$w.onReady(function () {
console.log('window ready....');
// give each item field the itemData key and value as text
const setDataForItem = (itemField, itemDataKey, itemDataValue) => {
itemField.text = `${itemDataKey}: ${itemDataValue}`
}
// todo
const cleanupRepeaters = () => {
}
$w("#historyRepeater").onItemReady(($item, itemDataObject) => {
// get all the fields to be populated
// TODO -- MAKE MORE
const dateField = $item("#date");
const itemFields = $item("#answer1,#answer2,#answer3,#answer4,#answer5,#answer6,#answer7");
// get an array of keys for the data
console.log(itemDataObject);
const dataKeys = Object.keys(itemDataObject);
// remove the keys and values that we don't need to display (system keys and values)
let filteredVals = [];
const filteredKeys = dataKeys.filter(key => key.charAt(0) !== "_");
filteredKeys.forEach(key => {
filteredVals.push(itemDataObject[key]);
})
// first we want to set the date if it's a date field
const date = new Date(itemDataObject["_createdDate"]);
dateField.text = `${date.getDate()}/${date.getMonth()}/${date.getFullYear()} at ${date.getHours()}:${date.getMinutes()}`;
// for each itemData, populate it in the corresponding field
itemFields.forEach((itemField, index) => {
if (index < filteredKeys.length) {
setDataForItem(itemField, filteredKeys[index], filteredVals[index]);
} else {
// if not populated, collapse the widget
itemField.collapse();
}
});
});
let memberId;
console.log('getting member data....');
currentMember.getMember().then(member => {
console.log('data retrieved....');
if (member) {
memberId = member['_id'];
console.log(`memberId: ${memberId}`);
}
console.log('querying data....');
wixData.query("JournalEntries")
.eq("_owner", memberId)
.descending("_createdDate")
.find().then(sortedEntries => {
console.log('data retrieved....');
// set the repeater data, triggering the creation of new items
console.log('setting repeater data....');
$w("#historyRepeater").data = sortedEntries.items;
})
});
});