Categories
JavaScript Answers

How to write formatted JSON in Node.js?

Sometimes, we want to write formatted JSON in Node.js.

In this article, we’ll look at how to write formatted JSON in Node.js.

How to write formatted JSON in Node.js?

To write formatted JSON in Node.js, we can use the JSON.stringify method.

For instance, we write

const output = JSON.parse(json);
const printToFile = JSON.stringify(output, null, "\t");

to call JSON.parse to parse the json JSON string into an object.

Then we call JSON.stringigfy with output and '\t' to return a JSON string that has indentation with tabs.

Conclusion

To write formatted JSON in Node.js, we can use the JSON.stringify method.

Categories
JavaScript Answers

How to calculate page load time in JavaScript?

Sometimes, we want to calculate page load time in JavaScript.

In this article, we’ll look at how to calculate page load time in JavaScript.

How to calculate page load time in JavaScript?

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.

For instance, we write

const loadTime =
  window.performance.timing.domContentLoadedEventEnd -
  window.performance.timing.navigationStart;

window.performance.timing.domContentLoadedEventEnd is the timestamp when DOM finished loading.

And window.performance.timing.navigationStart is the timestamp when navigation started.

We subtract window.performance.timing.domContentLoadedEventEnd by window.performance.timing.navigationStart to get the page load time in milliseconds.

Conclusion

To calculate page load time in JavaScript, we can use properties in the window.performance.timing property.

Categories
JavaScript Answers

How to fix the Expected ‘this’ to be used by class method error with JavaScript?

Sometimes, we want to fix the Expected ‘this’ to be used by class method error with JavaScript.

In this article, we’ll look at how to fix the Expected ‘this’ to be used by class method error with JavaScript.

How to fix the Expected ‘this’ to be used by class method error with JavaScript?

To fix the Expected ‘this’ to be used by class method error with JavaScript, we should change the function causing the error to an arrow function.

For instance, we write

const getUrlParams = (queryString) => {
  //...
};

to define the getUrlParams as an arrow function.

Then if it doesn’t reference this, then the error would go away.

Conclusion

To fix the Expected ‘this’ to be used by class method error with JavaScript, we should change the function causing the error to an arrow function.

Categories
JavaScript Answers

How to use Axios Interceptors retry original request and access original promise with JavaScript?

Sometimes, we want to use Axios Interceptors retry original request and access original promise with JavaScript.

In this article, we’ll look at how to use Axios Interceptors retry original request and access original promise with JavaScript.

How to use Axios Interceptors retry original request and access original promise with JavaScript?

To use Axios Interceptors retry original request and access original promise with JavaScript, we can retry the original request after we get the refresh token in the error handler function.

For instance, we write

const refreshToken = async (store) => {
  if (store.state.auth.isRefreshing) {
    return store.state.auth.refreshingCall;
  }
  store.commit("auth/setRefreshingState", true);
  const {
    data: { token },
  } = await Axios.get("get token");
  store.commit("auth/setToken", token);
  store.commit("auth/setRefreshingState", false);
  store.commit("auth/setRefreshingCall", undefined);
  store.commit("auth/setRefreshingCall", refreshingCall);
  return Promise.resolve(true);
};

Axios.interceptors.response.use(
  (response) => response,
  async (error) => {
    const status = error.response ? error.response.status : null;

    if (status === 401) {
      await refreshToken(store);
      error.config.headers["Authorization"] =
        "Bearer " + store.state.auth.token;
      error.config.baseURL = undefined;
      await Axios.request(error.config);
    }

    return Promise.reject(error);
  }
);

to define the refreshToken function.

It it, we make a get request with Axios.get to get the refresh token from token.

Then in the error handler that we call Axios.interceptors.response.use with, we check if the response status is 401.

If it is, then we call refreshToken to get the refresh token.

And then we set the token as the value of error.config.headers["Authorization"].

Then we call Axios.request to make the request with the error.config.

We then call Promise.reject to reject the promise.

Conclusion

To use Axios Interceptors retry original request and access original promise with JavaScript, we can retry the original request after we get the refresh token in the error handler function.

Categories
JavaScript Answers

How to make array indexOf() case insensitive with JavaScript?

Sometimes, we want to make array indexOf() case insensitive with JavaScript.

In this article, we’ll look at how to make array indexOf() case insensitive with JavaScript.

How to make array indexOf() case insensitive with JavaScript?

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.

For instance, we write

const array = ["I", "hAve", "theSe", "ITEMs"];
const index = array.findIndex((item) => q.toLowerCase() === item.toLowerCase());

to call array.findIndex with a callback that converts the item in array to lower case with toLowerCase.

And we compare it with value q after converting it to lower case.

Then the index of the array item that matches q in a case insensitive manner is returned.

Conclusion

To make array indexOf() case insensitive with JavaScript, we can use the array findIndex method.