Sorry, because I know this must be easier than I am making it, but I am trying to set a value in a database based on a column with a number.
In general, I am trying to set a flag based on the rollover current month which I have represented in a database by number 1-12.
I am able to obtain and generate the day of the month and the month, but don't seem to be able to filter and set another value based on that. I am not sure if the problem is having to do with filter by a numeric (so I created text equivalent fields) but I still have problems trying to use functions that all seem to take strings.
Here's my code - I've tried a lot of variations, creating text equivalent columns (though I'd rather just search and filter on a number) and for comparison purposes, how do I convert a number to a string.
I'm not sure I'm going about any of this the right way.
Would very much appreciate some steering.
Thank you!
import wixData from "wix-data"; var d = new Date(); var Month = (d.getMonth()+1) var Day = (d.getDate()) $w.onReady(function () { var OldMonth = 0;
// my modulo function if (Month ===1) OldMonth = 12; else OldMonth = Month - 1; if(Day === 1){ //if the month has rolled over, reset a flag in the database - e.g., set the featured month column as a "yes" and clear the previous month //set Old Month featured column to "" //set New Month featured column to "yes"
$w("#dataset10").setFilter(wixData.filter().contains("textmonth", "" + Month)).testfeature = "yes"; $w("#dataset10").setFilter(wixData.filter().contains("textmonth", "" + OldMonth)).testfeature = ""; }
});
Yes, you can filter by number if you want. As you can see from the API documentation, the propertyName (collection field) is a string, but the filter value is of whatever type the field is.
As an example, read the .eq() filter documentation which explains what the values can be.
Converst from NUMBER to STRING....
var myString = "11" convertedString = Number(myString) console.log(convertedString) console.log(typeof convertedString)
back to STRING....
var myNumber = 11 convertedNumber = (myNumber).toString console.log(convertedNumber) console.log(typeof convertedNumber)
Thank you for your quick reply! When you are performing the filter, can you nest it within the setfieldvalue() function? Or do you have to somehow assign it to a temporary location/pointer and then do the setfieldvalue () function to operate on that? I've looked for an example of this type but haven't found one to study.
Also is there an easy way to convert a numeric value to a text string for use in filtering? I would prefer to filter on a numeric value but the filter function operators all seem to require strings.
Thank you for any pointers! Much appreciated!
First you need to perform the filter which will get the correct item. You then can change a field in the current dataset item (the one that you got by filter) by using the setFieldValue() function. See the sample code snippets in the API.