Categories
JavaScript Answers

How to make fetch API work with CORS after OPTIONS response with JavaScript?

Sometimes, we want to make fetch API work with CORS after OPTIONS response with JavaScript.

In this article, we’ll look at how to make fetch API work with CORS after OPTIONS response with JavaScript.

How to make fetch API work with CORS after OPTIONS response with JavaScript?

To make fetch API work with CORS after OPTIONS response with JavaScript, we can just call fetch.

For instance, we write:

(async () => {
  const response = await fetch("https://jsonplaceholder.typicode.com/posts")
  const data = await response.json()
  console.log(data)
})()

We make a GET request with fetch.

Then we get the JSON response body with response.json.

Conclusion

To make fetch API work with CORS after OPTIONS response with JavaScript, we can just call fetch.

Categories
JavaScript Answers

How to determine if an object property exists and is not empty with JavaScript?

Sometimes, we want to determine if an object property exists and is not empty with JavaScript.

In this article, we’ll look at how to determine if an object property exists and is not empty with JavaScript.

How to determine if an object property exists and is not empty with JavaScript?

To determine if an object property exists and is not empty with JavaScript, we can use the hasOwnProperty method.

For instance, we write:

const errors = {
  error1: "Error 1 description",
  error2: "Error 2 description",
  error3: "",
  error4: "Error 4 description"
};

if (errors.hasOwnProperty('error2') && errors.error2 !== undefined) {
  // ...
}

We call errors.hasOwnProperty with 'error2' to check if the error2 property is in errors.

And then we check if errors.error2isn't set toundefinedwitherrors.error2 !== undefined`.

Conclusion

To determine if an object property exists and is not empty with JavaScript, we can use the hasOwnProperty method.

Categories
JavaScript Answers

How to get the last item of array without pop with JavaScript?

Sometimes, we want to get the last item of array without pop with JavaScript.

In this article, we’ll look at how to get the last item of array without pop with JavaScript.

How to get the last item of array without pop with JavaScript?

To get the last item of array without pop with JavaScript, we can use the array at method.

For instance, we write:

const array = [1, 2, 3, 5, 7];
const last = array.at(-1)
console.log(last)

We call array.at with -1 to return the last entry of array.

Therefore, last is 7.

Conclusion

To get the last item of array without pop with JavaScript, we can use the array at method.

Categories
JavaScript Answers

How to throw exception from async function with JavaScript?

Sometimes, we want to throw exception from async function with JavaScript.

In this article, we’ll look at how to throw exception from async function with JavaScript.

How to throw exception from async function with JavaScript?

To throw exception from async function with JavaScript, we can use the throw keyword.

For instance, we write:

(async () => {
  try {
    await Promise.reject('error')
  } catch (e) {
    throw e;
  }
})()

We use the throw keyword to throw the error object e.

And we’ll see the error logged in the console.

Conclusion

To throw exception from async function with JavaScript, we can use the throw keyword.

Categories
JavaScript Answers

How to fix body data not sent with a Axios request with JavaScript?

Sometimes, we want to fix body data not sent with a Axios request with JavaScript.

In this article, we’ll look at how to fix body data not sent with a Axios request with JavaScript.

How to fix body data not sent with a Axios request with JavaScript?

To fix body data not sent with a Axios request with JavaScript, we can call axios.request with an object with the data property set to an object with the data.

For instance, we write:

axios.request({
  method: 'POST',
  url: `https://jsonplaceholder.typicode.com/posts`,
  headers: {
    'Authorization': 'token'
  },
  data: {
    title: 'abc'
  },

})

to call axios.request with an object with the data property set to the JSON object request payload.

The header property has the request header data.

Conclusion

To fix body data not sent with a Axios request with JavaScript, we can call axios.request with an object with the data property set to an object with the data.