I am new to Wix and Corvid.
I am attempting to filter a database by two fields - month and day. This should give me only one result from the database.
I then want to read the values for those fields and assign them to text fields on the page. (It get more complicated than that, but I am trying to use the most stripped down code to understand how this works.)
In looking over many posts of this type, I have come up with the following code:
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dataset1").onReady(() => {
let filter = wixData.filter()
$w("#dataset1").setFilter(
filter.eq("month", 1)
);
let itemObj = $w("#dataset1").getCurrentItem();
$w("#texttempmonth2").text = itemObj.month.toLocaleString();
$w("#texttempday2").text = itemObj.day.toLocaleString();
});
});
Of course, this code doesn't work or else I wouldn't be posting here. The filter does not appear to be filtering because the value that I am getting for #texttempmonth2 is the first value in my database, not the one I should be getting if the filter worked properly.
Also, I am not clear on how to filter by two criteria - by my fields "month" and "day"
Any help would be appreciated.
Thanks for your help. I have modified my code to this:
import wixData from 'wix-data';
$w.onReady(function () {
$w("#dataset1").onReady(() => {
$w("#dataset1").setFilter(wixData.filter()
.eq("month", currentMonth)
.eq("day", currentDay)
)
let itemObj = $w("#dataset1").getCurrentItem();
$w("#texttempmonth2").text = itemObj.month.toLocaleString();
$w("#texttempday2").text = itemObj.day.toLocaleString();
});
});
The data that I am getting from the database is still the first record, not the only record that should be in the filtered results. (Note: I have one record for each day of the year. Since I am trying to filter by one month and one day variable, I should only be getting the record that matches today's month and day.)
Any further assistance would be appreciated.
First of all, I can see that you have added a filter() function before actually setting a filter with setFilter(). You can find the examples of how it should be done here: https://www.wix.com/corvid/reference/wix-dataset.Dataset.html#setFilter
As for filtering by two criteria, please check out this reference: https://www.wix.com/corvid/reference/wix-data.WixDataQuery.html#and