Search on date smart field

Hey everyone,

I just created a Smart field containing multiple columns. One of these columns are dates. I can’t seem to be able search dates.

Expected behavior

Being able to search smart fields having dates

Actual behavior

I get an error.

And this is my code:

let s = models.sequelize;
let split = search.split(" ");

let tableDateSearchConditions = split.map((x) => {
  return {
    tableDate: { [Op.like]: `%${x}%` },
  };
});

let searchCondition = {
  [Op.or]: [].concat(tableDateSearchConditions),
};

query.where[Op.and] = query.where[Op.and].concat(searchCondition);


return query;

Failure Logs

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: %2020%, _f: undefined, _strict: undefined, _locale: [object Object]
Error

Would any of you know how I can query dates correctly ?

Hi @daweido !
Can you share the format of the dates you are trying to filter ?

Hi @anon94532230,

Thanks for replying so quickly.

Here is my field’s get function.

get: (table) => {
				return models.restaurants
					.findOne({
						where: { restaurantId: table.restaurantIdKey },
					})
					.then((restaurant) => {
						return `${restaurant.restaurantName} | ${moment(
							table.tableDate
						).format("L - LT")} | ${table.tableAvailableSeats}`;
					});

I think it might be related to the format of the date, I’m not sure sequelize support this one :thinking:
Can you try with something like this ? YYYY-MM-DD HH:mm:ss

Thank you.

The same error still occurs. Does this have to do with the fact that I am using Momentjs to format the date?

I found this post: https://stackoverflow.com/a/58759791/7996564
It looks like your error, can you try to pass the date input format to moment ?

1 Like

Thank you @anon94532230.

I was able to implement it correctly, I just had to change the way I did my search.

Here is my code if anyone needs it:

get: (table) => {
				return models.restaurants
					.findOne({
						where: { restaurantId: table.restaurantIdKey },
					})
					.then((restaurant) => {
						return `${restaurant.restaurantName} | ${moment(
							table.tableDate
						).format("L - LT")} | ${table.tableAvailableSeats}`;
					});
			},
			search: function (query, search) {
				query.include.push({
					model: models.restaurants,
					as: "restaurant",
				});

				let s = models.sequelize;
				let split = search.split("|");
				const searchConditions = [];
				const splitLength = split.length;

				if (splitLength >= 1 && split[0].length > 0) {
					searchConditions.push(
						s.literal(`restaurant.restaurantName LIKE '%${split[0].trim()}%'`)
					);
				}

				if (splitLength >= 2 && split[1].length > 0) {
					searchConditions.push({
						tableDate: {
							[Op.like]: `%${moment(split[1].trim(), "L - LT").format(
								"L - LT"
							)}%`,
						},
					});
				}

				if (splitLength >= 3 && split[2].length > 0) {
					searchConditions.push({
						tableAvailableSeats: { [Op.eq]: split[2].trim() },
					});
				}

				let searchCondition = {
					[Op.and]: searchConditions,
				};

				query.where[Op.and] = query.where[Op.and].concat(searchCondition);

				return query;
			},
2 Likes