Categories
JavaScript Answers

How to get elements from a HTMLCollection with JavaScript?

Sometimes, we want to get elements from a HTMLCollection with JavaScript.

In this article, we’ll look at how to get elements from a HTMLCollection with JavaScript.

How to get elements from a HTMLCollection with JavaScript?

To get elements from a HTMLCollection with JavaScript, we can access them by their index.

For instance, we write:

<div class='review'>

</div>
<div class='review'>

</div>

to add elements with a class.

Then we write:

const els = document.getElementsByClassName('review');
console.log(els[0])

to call getElementsByClassName with 'review' to select elements with class review.

Then we can get the first one in the collection with els[0].

Conclusion

To get elements from a HTMLCollection with JavaScript, we can access them by their index.

Categories
JavaScript Answers Nodejs

How to fix ‘Error: Failed to lookup view “error” in views directory’ with Node.js and Express?

Sometimes, we want to fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express.

In this article, we’ll look at how to fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express.

For instance, we write:

const express = require('express')
const app = express()
const port = 3000

app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));

app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

to set the location of the view files with

app.set('views', path.join(__dirname, 'views'));

We may also set the static asset folder location with

app.use(express.static(path.join(__dirname, 'public')));

And we set the template engine to render the views with

app.engine('html', require('ejs').renderFile);

Conclusion

To fix ‘Error: Failed to lookup view "error" in views directory’ with Node.js and Express, we should set 'views' setting to the folder with the views.

Categories
JavaScript Answers

How to filter a complex JSON object using JavaScript?

Sometimes, we want to filter a complex JSON object using JavaScript.

In this article, we’ll look at how to filter a complex JSON object using JavaScript.

How to filter a complex JSON object using JavaScript?

To filter a complex JSON object using JavaScript, we can use the array filter method.

For instance, we write:

const obj = {
  "Lofts": "none",
  "Maisons": "2",
  "homes": [{
    "home_id": "1",
    "price": "925",
    "num_of_beds": "2"
  }, {
    "home_id": "2",
    "price": "1425",
    "num_of_beds": "4",
  }, {
    "home_id": "3",
    "price": "333",
    "num_of_beds": "5",
  }]
};

const filtered = obj.homes.filter((a) => {
  return a.home_id === '2';
});
console.log(filtered)

to call obj.homes.filter with a callback to return an array with the object with home_id property equal to 2.

As a result, filtered is

[{
  home_id: "2",
  num_of_beds: "4",
  price: "1425"
}]

Conclusion

To filter a complex JSON object using JavaScript, we can use the array filter method.

Categories
JavaScript Answers

How to fix className styles not working in React and JavaScript?

Sometimes, we want to fix className styles not working in React and JavaScript.

In this article, we’ll look at how to fix className styles not working in React and JavaScript.

How to fix className styles not working in React and JavaScript?

To fix className styles not working in React and JavaScript, we can import the styles file as a module.

For instance, we write:

styles.module.scss
.app {
  font-family: sans-serif;
  text-align: center;
}

to add some classes with styles into a styles file.

Then in the same folder, we write:

App.js
import React from "react";
import styles from "./styles.module.scss";

export default function App() {
  return <div className={styles.app}>hello world</div>;
}

to import the styles module as a default export.

Next, we reference the app class in styles.module.scss with styles.app.

Conclusion

To fix className styles not working in React and JavaScript, we can import the styles file as a module.

Categories
JavaScript Answers

How to get promise value in React and JavaScript?

Sometimes, we want to get promise value in React and JavaScript.

In this article, we’ll look at how to get promise value in React and JavaScript.

How to get promise value in React and JavaScript?

To get promise value in React and JavaScript, we can use await.

For instance, we write:

import React from "react";

export default function App() {
  const [ans, setAns] = React.useState();

  const getAnswer = async () => {
    const res = await fetch("https://yesno.wtf/api");
    const data = await res.json();
    setAns(data);
  };

  React.useEffect(() => {
    getAnswer();
  }, []);

  return <>{JSON.stringify(ans)}</>;
}

to create the getAnswer function that calls fetch with await to get the response data from the promise returned by fetch.

Likewise, we do the same with the json method.

And then we call setAns to set the value of ans.

Next, we call getAnswer in the useEffect callback to call when the component mounts.

And finally, we render ans in the return statement.

Conclusion

To get promise value in React and JavaScript, we can use await.