Filter column image based on today's date

@stedmanmp There is an example of multiple conditions in the setFilter documentation .

@tony-brunsman ah yes I see, I managed to fix it by moving things around. I’ll keep persevering!

@tony-brunsman

  1. import wixData from ‘wix-data’;

  2. $w.onReady( function wixData.query(“#dataset1”))

  3. $w(“#dataset1”).setFilter(wixData.filter()

  4. .ge(“owner”, “current.user”)

  5. .ge(“createdDate”, “today()”)

  6. .then((results) => {

  7. if (results.items.length > 0)

  8. $w(‘#button1’).collapse()

  9. else

  10. $w(‘#button1’).expand()

  11. }));

This is what i have put together but it does not work, try not to laugh it was the first code i have attempted to write… any help would be appreciated. Thanks

@stedmanmp No laughs here. There’s a certain audacity, tenacity, and bravery involved with plunging into something new. I respect that.

The setFilter function doesn’t return a results object. You can, however, use a “.then” to verify that the filter was applied and then see if any records met the condition.

Remember that when you assign a variable, you later reference that variable without quotes. In filtering and query conditions, field names (keys) are put in quotes.

I’ve revised the code.

import wixData from 'wix-data';
import wixUsers from 'wix-users';

// Get a date object to work with.
let today = new Date();
// Need to get the possible second of the day and the latest 
// because the date writes to the field that way.  Will use the
// between function in the filtering code below to poll for any
// submission that day for that user.
let yearValue = today.getFullYear();
let monthValue = today.getMonth();
let dayValue = today.getDate();
let dateValue1 = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
let dateValue2 = new Date(yearValue, monthValue, dayValue, 23, 59, 59);

let user = wixUsers.currentUser;

$w.onReady(function () {
    $w("#dataset1").setFilter(wixData.filter()
    .eq("_owner", user.id)
    .between("_createdDate", dateValue1, dateValue2)
    )
    .then(() => {
        let recCount = $w('#dataset1').getTotalCount();
        console.log("recCount follows");
        console.log(recCount);
        if (recCount > 0){
            $w('#button1').collapse()
        } else {
            $w('#button1').expand()
        }
    })
});

@tony-brunsman Thank you very much, i have added the code but it hasn’t done anything, could anything else i have on there be causing problems? i have no errors but nothing happens, i made a user account on the live website and i am still able to submit more than once

Sorry for being a pain

// For full API documentation, including code examples, visit https://wix.to/94BuAAs
$w.onReady(function () {
//TODO: write your page related code here...
});
export function button1_click_1(event) {
if ($w('#box1').collapsed) {
$w('#box1').expand();
} else
$w('#box1').collapse();
}
export function button2_click_1(event) {
if ($w('#box2').collapsed) {
$w('#box2').expand();
} else
$w('#box2').collapse();
}
export function button3_click_1(event) {
if ($w('#box3').collapsed) {
$w('#box3').expand();
} else
$w('#box3').collapse();
}
export function button4_click_1(event) {
if ($w('#box4').collapsed) {
$w('#box4').expand();
} else
$w('#box4').collapse();
}
export function button5_click_1(event) {
if ($w('#box5').collapsed) {
$w('#box5').expand();
} else
$w('#box5').collapse();
}
export function button6_click_1(event) {
if ($w('#box6').collapsed) {
$w('#box6').expand();
} else
$w('#box6').collapse();
}
export function button7_click_1(event) {
if ($w('#box7').collapsed) {
$w('#box7').expand();
} else
$w('#box7').collapse();
}
export function button8_click_1(event) {
if ($w('#box8').collapsed) {
$w('#box8').expand();
} else
$w('#box8').collapse();
}
export function button9_click_1(event) {
if ($w('#box9').collapsed) {
$w('#box9').expand();
} else
$w('#box9').collapse();
}
import wixUsers from 'wix-users';
// ...
let user = wixUsers.currentUser;
let userId = user.id;
let isLoggedIn = user.loggedIn;
user.getEmail()
.then( (email) => {
let userEmail = email; // "user@something.com"
$w("#input2").value = userEmail;
}
);
import wixData from 'wix-data';
// Get a date object to work with.
let today = new Date();
// Need to get the possible second of the day and the latest
// because the date writes to the field that way.  Will use the
// between function in the filtering code below to poll for any
// submission that day for that user.
let yearValue = today.getFullYear();
let monthValue = today.getMonth();
let dayValue = today.getDate();
let dateValue1 = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
let dateValue2 = new Date(yearValue, monthValue, dayValue, 23, 59, 59);
$w.onReady(function () {
$w("#dataset1").setFilter(wixData.filter()
.eq("_owner", user.id)
.between("_createdDate", dateValue1, dateValue2)
)
.then(() => {
let recCount = $w('#dataset1').getTotalCount();
console.log("recCount follows");
console.log(recCount);
if (recCount > 0){
$w('#button11').collapse()
} else {
$w('#button11').expand()
}
})
});

@stedmanmp The nine button click code snippets should be outside of the onReady().

@tony-brunsman I have 2 onready, the initial one and the one in you code, should i delete the initial one to move the snippets out?

@stedmanmp If you haven’t already done so, yes, remove the first onReady, but shift all of those button click function blocks to the bottom of the page. The import statements and global (page) variable declarations ought to be at the top.

@tony-brunsman Could my code above be amended so that if the ‘date’ field of the record in the database is before today’s date, it changes the boolean value of ‘current’ to false. This would ensure that future images don’t display in the second gallery and automatically move today’s image of the day to gallery 2 tomorrow. Would this work?

@gavincoll It’s already should be doing that with the less than the current date (earliest time for that date) or the greater than the the current date (latest time for that date) condition. Do a little testing. Manually change the current field to true for some of those records with dates in the past, and see if you’re getting the correct results.

@tony-brunsman If i go into the collection and change the current value to false, it will display in 2nd gallery, and also if I change the value to true, it removes it from gallery 2.

However, with the code, I have tried combinations of less than, more than , true and false and there are no changes to the homepage. I have tried to apply to #dataset2 and also to “rigoftheday” which is the name of the collection

The console also shows that it didn’t update anything in the array

@gavincoll I’d be guessing if I said anything at this point. If you could upload a screen shot of the datetoimage collection, maybe that will give me a better idea.

@tony-brunsman Here’s a screenshot of the collection, the Current field is on the right. The collection is actually called ‘rigoftheday’ rather than ‘datetoimage’

Here is the code I have at the moment

import wixData from 'wix-data';
//calculateDate;
$w.onReady(function () {
$w("#dataset1").onReady(() => {
let localDate = new Date();
let yearValue = localDate.getFullYear();
let monthValue = localDate.getMonth();
let dayValue = localDate.getDate();
let dateValue1 = new Date(yearValue, monthValue, dayValue, 0, 0, 0);
let dateValue2 = new Date(yearValue, monthValue, dayValue, 23, 59, 59);
$w("#dataset1").setFilter(wixData.filter()
.between('date', dateValue1, dateValue2)
);


wixData.query('rigoftheday')
.lt('date', dateValue1)
.or(
wixData.query('rigoftheday')
.gt('date', dateValue2)
)
.find()
.then(results => {
console.log(results);
let updateArray = [];
// Loop through results of those that are true
// and out-of-date, change the value to false
// and then add the item to the updateArray.
for (var i = 0; i < results.items.length; i++) {
let thisItem = results.items[i];
if (thisItem.current) {
thisItem.current = false;
updateArray.push(thisItem);
}



}
// Now, update the collection records with this array of
wixData.bulkUpdate('date', updateArray)
.then((bulkresults) => {
// this will return an array showing the IDs of the
// updated records.
console.log(bulkresults);
})
.catch((err) => {
console.log(err);
});
}
);
});
});


@gavincoll I want to make sure that we are both testing this in the same way. With the rigoftheday collection, I would manually change the current value to true for the dates that have passed. I’m doing this on a collection that houses book data, run the same code with different collection and field names, and it does indeed change the current field to false for all of the records that have a date that has passed.

Does the first query return anything?

If you are testing this the same way and console.log(bulkresults) doesn’t show that it updated anything, it could mean that there is a permissions issue.

Attached is the photo of the log. I even set the permissions of the collection to be ‘Anyone’ for all categories

Are you testing it in the same way?

The screen shot shows one date field called “dateStandard”. In the query, the name of the field is “date”. That would be a reason why it would return no results.

The fieldkey of dateStandard is date, that’s why I’ve been using that. Is that correct?

i I have managed to get the array to show 6 in the console but from what I can see, there is no change to the ‘current’ value when I try on either false or true