Categories
JavaScript Answers

How to save session only cookies with JavaScript?

Sometimes, we want to save session only cookies with JavaScript.

In this article, we’ll look at how to save session only cookies with JavaScript.

How to save session only cookies with JavaScript?

To save session only cookies with JavaScript, we can use session storage instead of cookies.

For instance, we write

const myVariable = { a: [1, 2, 3, 4], b: "some text" };

sessionStorage["myVariable"] = JSON.stringify(myVariable);
const readValue = JSON.parse(sessionStorage["myVariable"]);

to save an entry with key myVariable into session storage with

sessionStorage["myVariable"] = JSON.stringify(myVariable);

Since myVariable is an object, we’ve convert it to a string with JSON.stringify before we can save the value.

Then we read the value by the key with

sessionStorage["myVariable"]

Then we call JSON.parse to parse the stringified JSON object into a JavaScript object.

Conclusion

To save session only cookies with JavaScript, we can use session storage instead of cookies.

Categories
JavaScript Answers

How to convert relative path to absolute with Node.js?

Sometimes, we want to convert relative path to absolute with Node.js.

In this article, we’ll look at how to convert relative path to absolute with Node.js.

How to convert relative path to absolute with Node.js?

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

For instance, we write

const { resolve } = require("path");

const absPath = resolve("../../bb/tmp.txt");

to call resolve with a relative path to return a string with the equivalent absolute path.

Conclusion

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

Categories
JavaScript Answers

How to add Sequelize table without column ‘id’ with JavaScript?

Sometimes, we want to add Sequelize table without column ‘id’ with JavaScript.

In this article, we’ll look at how to add Sequelize table without column ‘id’ with JavaScript.

How to add Sequelize table without column ‘id’ with JavaScript?

To add Sequelize table without column ‘id’ with JavaScript, we define a table with our own primary key column.

For instance, we write

const AcademyModule = sequelize.define(
  "academy_module",
  {
    academy_id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
    },
    module_id: DataTypes.INTEGER,
    module_module_type_id: DataTypes.INTEGER,
    sort_number: DataTypes.INTEGER,
    requirements_id: DataTypes.INTEGER,
  },
  {
    freezeTableName: true,
  }
);

to call sequelize.define with the table name and an object with the column names and their types.

We add the academy_id primary key column so the id column won’t be added automatically.

Conclusion

To add Sequelize table without column ‘id’ with JavaScript, we define a table with our own primary key column.

Categories
JavaScript Answers

How to add multiple markers with Infowindows with Google Maps API?

Sometimes, we want to add multiple markers with Infowindows with Google Maps API.

In this article, we’ll look at how to add multiple markers with Infowindows with Google Maps API.

How to add multiple markers with Infowindows with Google Maps API?

To add multiple markers with Infowindows with Google Maps API, we can create a function that returns the mouseover listener callback.

For instance, we write

const infowindow = new google.maps.InfoWindow();

google.maps.event.addListener(
  marker,
  "mouseover",
  ((marker) => {
    return () => {
      const content = address;
      infowindow.setContent(content);
      infowindow.open(map, marker);
    };
  })(marker)
);

to use the InfoWindow constructor to create an infoWindow.

Then we call addListener to add a mouseover event listener to the marker.

Then callback is the function returned by calling the anonymous function with marker.

The function sets the content of the infoWindow and open on the map over the marker with open.

Conclusion

To add multiple markers with Infowindows with Google Maps API, we can create a function that returns the mouseover listener callback.

Categories
JavaScript Answers

How to load JSON data from local file into React?

Sometimes, we want to load JSON data from local file into React.

In this article, we’ll look at how to load JSON data from local file into React.

How to load JSON data from local file into React?

To load JSON data from local file into React, we can import them directly.

For instance, we write

import myData from "./data.json";

to import the JSON object in data.json as the myData object.

Conclusion

To load JSON data from local file into React, we can import them directly.