Categories
JavaScript Answers

How to fix Mongoose findOneAndUpdate doesn’t return updated document?

Sometimes, we want to fix Mongoose findOneAndUpdate doesn’t return updated document.

In this article, we’ll look at how to fix Mongoose findOneAndUpdate doesn’t return updated document.

How to fix Mongoose findOneAndUpdate doesn’t return updated document?

To fix Mongoose findOneAndUpdate doesn’t return updated document, we call findOneAndUoate with the new option set to true.

for instance, we write

Cat.findOneAndUpdate({
  age: 100
}, {
  $set: {
    name: "jane"
  }
}, {
  new: true
}, (err, doc) => {
  if (err) {
    console.log("Something wrong when updating data!");
  }
  console.log(doc);
});

to call findOneAndUpdate with an object that sets new to true to return the new document as the value of doc.

We set name to 'jane' where the Cat entry has age value 100.

Conclusion

To fix Mongoose findOneAndUpdate doesn’t return updated document, we call findOneAndUoate with the new option set to true.

Categories
JavaScript Answers

How to get file name from absolute path with Node.js?

Sometimes, we want to get file name from absolute path with Node.js.

In this article, we’ll look at how to get file name from absolute path with Node.js.

How to get file name from absolute path with Node.js?

To get file name from absolute path with Node.js, we can use the path.basename method.

For instance, we write

const path = require("path");
const fileName = "C:\\Python27\\ArcGIS10.2\\python.exe";
const file = path.basename(fileName);

to call path.basename with the fileName path string.

As a result, file is 'python.exe'.

Conclusion

To get file name from absolute path with Node.js, we can use the path.basename method.

Categories
JavaScript Answers

How to URl encode strings in Node.js?

Sometimes, we want to URl encode strings in Node.js.

In this article, we’ll look at how to URl encode strings in Node.js.

How to URl encode strings in Node.js?

To URl encode strings in Node.js, we can call encodeURIComponent.

For instance, we write

encodeURIComponent('select * from table where uid = me()')

to call encodeURIComponent with return a string that is URI encoded.

Conclusion

To URl encode strings in Node.js, we can call encodeURIComponent.

Categories
JavaScript Answers

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

Sometimes, we want to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

In this article, we’ll look at how to fix ‘ReferenceError: fetch is not defined’ error in Node.js.

How to fix ‘ReferenceError: fetch is not defined’ error in Node.js?

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

To install it, we run

npm i node-fetch

Then we import it with

import fetch from "node-fetch";

so we can call fetch.

Conclusion

To fix ‘ReferenceError: fetch is not defined’ error in Node.js, we can install node-fetch.

Categories
JavaScript Answers

How to measure the execution time of Node.js code with callbacks?

Sometimes, we want to measure the execution time of Node.js code with callbacks.

In this article, we’ll look at how to measure the execution time of Node.js code with callbacks.

How to measure the execution time of Node.js code with callbacks?

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.

For instance, we write

let start = process.hrtime();

const elapsedTime = (note) => {
  const precision = 3;
  const [startTime, endTime] = process.hrtime(start)
  const elapsed = endTime / 1000000;
  console.log(startTime, elapsed);
  start = process.hrtime();
}

to create the elaspedTime function tyat calls process.hrtime with start to return the timestamp of the elasped time and assign that to endTime.

The endTime is in nanoseconds.

So we divide endTime by 1000000 to get the elasped time in seconds.

Conclusion

To measure the execution time of Node.js code with callbacks, we can use the process.hrtime method.