Categories
JavaScript Answers

How to delete document from Firestore using where clause with JavaScript?

Sometimes, we want to delete document from Firestore using where clause with JavaScript.

In this article, we’ll look at how to delete document from Firestore using where clause with JavaScript.

How to delete document from Firestore using where clause with JavaScript?

To delete document from Firestore using where clause with JavaScript, we can use the delete method.

For instance, we write

const jobSkills = await store
  .collection("job_skills")
  .where("job_id", "==", post.job_id)
  .get();

const batch = store.batch();

jobSkills.forEach((doc) => {
  batch.delete(doc.ref);
});

await batch.commit();

to get the entries we want to delete with where and get.

We call batch to catch the delete operations.

Then we call forEach with a callback that calls batch.delete to delete the entry by referencing it with doc.ref.

Next we call batch.commit to commit the delete operations.

Conclusion

To delete document from Firestore using where clause with JavaScript, we can use the delete method.

Categories
JavaScript Answers

How to convert MongoDB ObjectID to string in JavaScript?

Sometimes, we want to convert MongoDB ObjectID to string in JavaScript.

In this article, we’ll look at how to convert MongoDB ObjectID to string in JavaScript.

How to convert MongoDB ObjectID to string in JavaScript?

To convert MongoDB ObjectID to string in JavaScript, we use the str property.

For instance, we write

const s = ObjectId("507f191e810c19729de860ea").str;

to create an object ID with ObjectId.

Then we get the string content of the object ID with str.

Conclusion

To convert MongoDB ObjectID to string in JavaScript, we use the str property.

Categories
JavaScript Answers

How to replace multiple strings at once with JavaScript?

Sometimes, we want to replace multiple strings at once with JavaScript.

In this article, we’ll look at how to replace multiple strings at once with JavaScript.

How to replace multiple strings at once with JavaScript?

To replace multiple strings at once with JavaScript, we can use the string replace method.

For instance, we write

const findFor = ["<", ">", "\n"];
const replaceWith = ["&lt;", "&gt;", "<br/>"];
const originalString = "<strong>Hello World</strong> \n Let's code";
let modifiedString = originalString;

findFor.forEach(
  (tag, i) =>
    (modifiedString = modifiedString.replace(
      new RegExp(tag, "g"),
      replaceWith[i]
    ))
);

to call findFor.forEach with a callback that replaces the tag substring in modifiedString with the replaceWith[i] string.

i is the index of the findFor array being looped through.

We use the new RegExp(tag, "g") regex to find all matches of tag.

Conclusion

To replace multiple strings at once with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to check if geolocation has been declined with JavaScript?

Sometimes, we want to check if geolocation has been declined with JavaScript.

In this article, we’ll look at how to check if geolocation has been declined with JavaScript.

How to check if geolocation has been declined with JavaScript?

To check if geolocation has been declined with JavaScript, we can use the navigator.permissions.query method.

For instance, we write

const result = await navigator.permissions.query({ name: "geolocation" });
console.log(result.state);

to call navigator.permissions.query with an object with the name property set to 'geolocation' to check if the app has permission to use geolocation.

It returns a promise that has whether permission is granted or not.

If result.state is 'granted', then permission is granted.

Conclusion

To check if geolocation has been declined with JavaScript, we can use the navigator.permissions.query method.

Categories
JavaScript Answers

How to handle an if statement in a Mustache template with JavaScript?

Sometimes, we want to handle an if statement in a Mustache template with JavaScript.

In this article, we’ll look at how to handle an if statement in a Mustache template with JavaScript.

How to handle an if statement in a Mustache template with JavaScript?

To handle an if statement in a Mustache template with JavaScript, we can use some built in tags.

For instance, we write

{{#value}}
  value is true
{{/value}}
{{^value}}
  value is false
{{/value}}

to render ‘value is true’ when value is true with

{{#value}}
  value is true
{{/value}}

Likewise, we render ‘value is false’ when value is false with

{{^value}}
  value is false
{{/value}}

Conclusion

To handle an if statement in a Mustache template with JavaScript, we can use some built in tags.