Categories
JavaScript Answers

How to store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript?

Sometimes, we want to store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript.

In this article, we’ll look at how to store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript.

How to store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript?

To store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript, we can use the version of readFile that returns a promise.

For instance, we write:

const { promises: fs } = require('fs')
const read = async () => {
  const file = await fs.readFile('file.txt')
  console.log(file.toString())
}
read()

to import the promises version of fs with const { promises: fs } = require('fs').

Then we call fs.readFile with the file path.

And we get the read file handle with await and assign it to file.

Then we convert the file content to a string with toString.

Conclusion

To store Node.js fs.readfile’s result in a variable and pass to global variable with JavaScript, we can use the version of readFile that returns a promise.

Categories
JavaScript Answers

How to replace undefined with a empty string with JavaScript?

Sometimes, we want to replace undefined with a empty string with JavaScript.

In this article, we’ll look at how to replace undefined with a empty string with JavaScript.

How to replace undefined with a empty string with JavaScript?

To replace undefined with a empty string with JavaScript, we can use the || operator to return a default if its left operand is undefined.

For instance, we write:

const s = undefined
const newS = s || 'hello'
console.log(newS)

We assign newS to s is s is truthy or 'hello' otherwise.

Since s is undefined, which is falsy, newSis‘hello’`.

Conclusion

To replace undefined with a empty string with JavaScript, we can use the || operator to return a default if its left operand is undefined.

Categories
JavaScript Answers

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

Sometimes, we want to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

In this article, we’ll look at how to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript.

How to fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript?

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

For instance, we write:

import React from "react";

export default function App() {
  return <div>hello world</div>;
}

to return one div.

Or:

import React from "react";

export default function App() {
  return <>hello world</>;
}

to return a fragment.

Conclusion

To fix the ‘A valid React element (or null) must be returned’ error with one of the component error in React and JavaScript, we should make sure our React component returns one root node or fragment.

Categories
JavaScript Answers

How to create random-salt-hash with crypto with Node.js and JavaScript?

Sometimes, we want to create random-salt-hash with crypto with Node.js and JavaScript.

In this article, we’ll look at how to create random-salt-hash with crypto with Node.js and JavaScript.

How to create random-salt-hash with crypto with Node.js and JavaScript?

To create random-salt-hash with crypto with Node.js and JavaScript, we can use the crypto.randomBytes method.

For instance, we write:

const crypto = require('crypto')
const buf = crypto.randomBytes(16);
console.log(buf)

We call crypto.randomBytes with 16 to create a buffer object with 16 random bytes.

Conclusion

To create random-salt-hash with crypto with Node.js and JavaScript, we can use the crypto.randomBytes method.

Categories
JavaScript Answers

How to extract image src from a string easily with JavaScript?

Sometimes, we want to extract image src from a string easily with JavaScript.

In this article, we’ll look at how to extract image src from a string easily with JavaScript.

How to extract image src from a string easily with JavaScript?

To extract image src from a string easily with JavaScript, we can create a div, set the innerHTML property of the div to the img element string.

And then we can select the img element from the div and get the src property from it.

For instance, we write:

const imgStr = '<img src="http://example.com/img.jpg">'

const getImgSrc = (imgStr) => {
  const div = document.createElement('div')
  div.innerHTML = imgStr
  const img = div.querySelector('img')
  return img.src
}

console.log(getImgSrc(imgStr))

to define the getImgSrc property to a function that takes the imgStr string.

In it, we create a div with createElement.

Then we set div.innerHTML to imgStr.

And then we call div.querySelector with 'img' to get the img element.

Finally, we return the img.src property.

As a result, the console log logs 'http://example.com/img.jpg'.

Conclusion

To extract image src from a string easily with JavaScript, we can create a div, set the innerHTML property of the div to the img element string.

And then we can select the img element from the div and get the src property from it.