Categories
JavaScript Answers

How to use Node.js MongoDB driver async/await queries with JavaScript?

Sometimes, we want to use Node.js MongoDB driver async/await queries with JavaScript.

In this article, we’ll look at how to use Node.js MongoDB driver async/await queries with JavaScript.

How to use Node.js MongoDB driver async/await queries with JavaScript?

To use Node.js MongoDB driver async/await queries with JavaScript, we use await before database methods.

For instance, we write

const queryDb = async (query) => {
  let db, client;
  try {
    client = await MongoClient.connect(process.env.MONGODB_CONNECTION_STRING, {
      useNewUrlParser: true,
    });
    db = client.db(dbName);
    return await db.collection(collectionName).find(query).toArray();
  } finally {
    client.close();
  }
};

to define the queryDb function.

In it, we call MongoClient.connect to connect to the database with the connect string.

It returns a promise so we use await to get the client.

Then we get the database with client.db.

Then we call find to find an object in the collectionName collection and get the results from the promise returned with await.

We call close to close the connection in the finally block.

Conclusion

To use Node.js MongoDB driver async/await queries with JavaScript, we use await before database methods.

Categories
JavaScript Answers

How to set default array values with JavaScript?

To set default array values with JavaScript, we call the fill method.

For instance, we write

const a = Array(20).fill(0);

to create an empty array with 20 slots with Array.

Then we call fill to fill all the empty slots with 0.

Conclusion

To set default array values with JavaScript, we call the fill method.

Categories
JavaScript Answers

How to remove emoji code using JavaScript?

Sometimes, we want to remove emoji code using JavaScript.

In this article, we’ll look at how to remove emoji code using JavaScript.

How to remove emoji code using JavaScript?

To remove emoji code using JavaScript, we use the replace method.

For instance, we write

const s = "Smile😀".replace(/\p{Emoji}/gu, "");

to call replace on "Smile😀" with the \p{Emoji} pattern to match emojis.

We use the g flag to replace all matches with empty strings.

And we use the u flag to match Unicode characters.

Conclusion

To remove emoji code using JavaScript, we use the replace method.

Categories
JavaScript Answers

How to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript?

Sometimes, we want to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript.

In this article, we’ll look at how to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript.

How to fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript?

To fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript, we should make sure the element we’re observing is available.

For instance, we write

const composeObserver = new MutationObserver(function (mutations) {
  mutations.forEach((mutation) => {
    mutation.addedNodes.forEach((node) => {
      //...
    });
  });
});

const composeBox = document.querySelector(".no");
if (composeBox) {
  const config = { childList: true };
  composeObserver.observe(composeBox, config);
}

to select the element to observer with querySelector.

Then if it’s not null, we call observe to observer the changes in the element.

Conclusion

To fix ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’ with JavaScript, we should make sure the element we’re observing is available.

Categories
JavaScript Answers

How to declare an array in JavaScript?

Sometimes, we want to declare an array in JavaScript.

In this article, we’ll look at how to declare an array in JavaScript.

How to declare an array in JavaScript?

To declare an array in JavaScript, we can use the array literal syntax.

For instance, we write

const x = [];
const y = [10];

to declare the empty array x.

And we declare the array y with 10 inside.

Conclusion

To declare an array in JavaScript, we can use the array literal syntax.