Categories
JavaScript Answers

How to query for entries with columns that are not null with JavaScript Sequelize?

To query for entries with columns that are not null with JavaScript Sequelize, we use the Op.ne property.

For instance, we write

Post.update(
  {
    updatedAt: null,
  },
  {
    where: {
      deletedAt: {
        [Op.ne]: null,
      },
    },
  }
);

to select the entries that have the deletedAt field set to a non-null value with Op.ne.

And then we call update with an object to set updatedAt to null for entries that match the condition.

Categories
JavaScript Answers

How to create the checkbox dynamically using JavaScript?

To create the checkbox dynamically using JavaScript, we use the createElement method.

For instance, we write

const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id";

to create an input element with createElement.

We set its type attribute to 'checkbox' to make it a checkbox by setting the type property.

Then we set its name, value and id attributes by setting the properties with the same name.

Categories
JavaScript Answers

How to define a regular expression for not allowing spaces in the input field with JavaScript?

To define a regular expression for not allowing spaces in the input field with JavaScript, we use the \S pattern.

For instance, we write

const regex = /^\S*$/;

to define a regex that matches any non-space characters with the \S+ pattern.

And we use ^ to match the start of a string and $ the end of a string.

We use * to match repetition.

Categories
JavaScript Answers

How to get formatted date time in YYYY-MM-DD HH:mm:ss format using JavaScript?

To get formatted date time in YYYY-MM-DD HH:mm:ss format using JavaScript, we can use some date methods.

For instance, we write

const getFormattedDate = () => {
  const date = new Date();
  const str =
    date.getFullYear() +
    "-" +
    (date.getMonth() + 1) +
    "-" +
    date.getDate() +
    " " +
    date.getHours() +
    ":" +
    date.getMinutes() +
    ":" +
    date.getSeconds();
  return str;
};

to call getFullYear to get the 4 digit year.

We call getMonth to get the month and add 1 to get the human readable month.

We call getDate to get the date.

We call getHours to get the hours.

We call getMinutes to get the minutes.

And we call getSeconds to get the seconds.

Then we concatenate the values together to return the formatted date.

Categories
JavaScript Answers

How to define regex for empty string or white space with JavaScript?

To define regex for empty string or white space with JavaScript, we can use the \s+ pattern.

For instance, we write

const regex = /^\s+$/;

to define a regex that matches a string with all spaces by using ^ to match the start of the string, \s+ to match spaces, and $ to match the end of the string.