Categories
JavaScript Answers

How to query Firestore database for document ID with JavaScript?

Sometimes, we want to query Firestore database for document ID with JavaScript.

In this article, we’ll look at how to query Firestore database for document ID with JavaScript.

How to query Firestore database for document ID with JavaScript?

To query Firestore database for document ID with JavaScript, we call the doc method.

For instance, we write

const book = db.collection("books").doc("fK3ddutEpD2qQqRMXNW5").get();

to get the books collection with

db.collection("books")

Then we call doc to get the entry from the books collection with ID 'fK3ddutEpD2qQqRMXNW5'.

Finally, we call get to return the object with the given ID.

Conclusion

To query Firestore database for document ID with JavaScript, we call the doc method.

Categories
JavaScript Answers

How to set img src with JavaScript?

Sometimes, we want to set img src with JavaScript.

In this article, we’ll look at how to set img src with JavaScript.

How to set img src with JavaScript?

To set img src with JavaScript, we set the src property of the img element.

For instance, we write

const image = document.createElement("img");
image.src = "https://picsum.photos/200/300";

to call createElement to create an img element.

Then we set its src attribute by setting the src property to the image URL.

Conclusion

To set img src with JavaScript, we set the src property of the img element.

Categories
JavaScript Answers

How to fix UTC gives wrong date with moment.js and JavaScript?

To fix UTC gives wrong date with moment.js and JavaScript, we should add the time zone to the datetime string we’re parsing into a moment object.

For instance, we write

const datetime = moment(new Date("07-18-2022 UTC"))
  .utc()
  .format("YYYY-MM-DD HH:mm");

to call moment with the new Date("07-18-2022 UTC") date, which we specified to be UTC.

Then we call utc to return a UTC moment.

Next, we call format to return a datetime string in specified format.

Categories
TypeScript Answers

How to find module “fs” in VS Code with TypeScript?

Sometimes, we want to find module "fs" in VS Code with TypeScript.

In this article, we’ll look at how to find module "fs" in VS Code with TypeScript.

How to find module "fs" in VS Code with TypeScript?

To find module "fs" in VS Code with TypeScript, we need to install the @types/node package.

To install it, we run

npm install --save-dev @types/node

in our project folder.

This will add the type definitions for built in Node.js modules into our Node TypeScript project.

Conclusion

To find module "fs" in VS Code with TypeScript, we need to install the @types/node package.

Categories
JavaScript Answers

How to use await outside of an async function with JavaScript?

Sometimes, we want to use await outside of an async function with JavaScript.

In this article, we’ll look at how to use await outside of an async function with JavaScript.

How to use await outside of an async function with JavaScript?

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.

For instance, we write

(async () => {
  await foo();
  // ...
})();

to define an async function.

Then we wrap it in parentheses and add () at the end to call it immediately after it’s defined.

Conclusion

To use await outside of an async function with JavaScript, we can call the async function right after we defined it.