Hi All,
I am having issues with a ‘on this day’ feature on my website. I did have some success a while ago but have lost the original thread and code.
Basically i want to display a text field and image from a database along with the current date on the main page of the [site.](site.
www.shipsofcalmac.co.uk)
[www.shipsofcalmac.co.uk](site.
www.shipsofcalmac.co.uk) is the main page - i also have everything listed on a separate page until i can get this feature working again… [www.shipsofcalmac.co.uk/on-this-day-1](https://www.shipsofcalmac.co.uk/on-this-day-1
Any)
Any help would be much appreciated!
Thanks!
Can you provide more information like database name and field key…
Probably you need to get the current date https://www.w3schools.com/js/js_dates.asp
…and then query the database for that date https://www.wix.com/corvid/reference/wix-data/wixdataquery/eq
Can you provide more info as I said
Hi Ajit, thank you for your reply - does this help…?
Regards,
Steve
@stevehurst85 From your picture, I think ‘month’ is the field which stores the month and ‘date’ is the field which stores the date on which the image and text should be show…
If it is →
$w.onReady(function () {
const today = new Date()
let month = today.toLocaleString('default', { month: 'long' })
let date = today.getDate();
wixData.query("OnThisDay")
.eq("month", month)
.eq("date", date)
.find()
.then((res) => {
if (res.items.length > 0) {
let items = res.items;
let photo = items[0].image; //image
let text = items[0].text //text
} else {
// handle case where no matching items found
}
});
});
@ajithkrr Hi, i think i see where i went wrong but it’s still not displaying the image or text on the main page in the two fields above…
You should pass the value to your photo and
.then((res) => {
if (res.items.length > 0) {
let items = res.items;
let photo = items[0].image;
$w('#image').src = photo;
let text = items[0].text
$w('#text').text = text;
} else {
// handle case where no matching items found
}
});
@ajithkrr
Ok, so i have this in the code and it’s still not working - do i need to create a link to the dataset on the page itself too?
I really appreciate your help!
import wixData from 'wix-data';
$w.onReady(function () {
const today = new Date()
let month = today.toLocaleString('default', { month: 'long' })
let date = today.getDate();
wixData.query("OnThisDay")
.eq("month", month)
.eq("date", date)
.find()
.then((res) => {
if (res.items.length > 0) {
let items = res.items;
let photo = items[0].image;
$w('#image').src = photo;
let text = items[0].text
$w('#text').text = text;
} else {
// handle case where no matching items found
}
});
});