Categories
JavaScript Answers

How to populate after save with Mongoose?

Sometimes, we want to populate after save with Mongoose.

In this article, we’ll look at how to populate after save with Mongoose.

How to populate after save with Mongoose?

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

For instance, we write

const t = new MyModel(value)
await t.save()
t.populate('my-path').execPopulate()

to call t.save to save t in the database.

Then we call t.populate with the property to populate.

And then we call execPopulate to run the populate operation.

Conclusion

To populate after save with Mongoose, we can use the populate and execPopulate method after calling save.

Categories
JavaScript Answers

How to get an accurate timestamp in Node.js?

Sometimes, we want to get an accurate timestamp in Node.js.

In this article, we’ll look at how to get an accurate timestamp in Node.js.

How to get an accurate timestamp in Node.js?

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

For instance, we write

process.hrtime.bigint()

to call process.hrtime.bigint to return the current timestamp in nanoseconds as a JavaScript bigint.

Conclusion

To get an accurate timestamp in Node.js, we can use the process.hrtime.bigint method.

Categories
JavaScript Answers

How to drop a database with Mongoose?

Sometimes, we want to drop a database with Mongoose.

In this article, we’ll look at how to drop a database with Mongoose.

How to drop a database with Mongoose?

To drop a database with Mongoose, we can remove all the content of a collection.

For instance, we write

Model.remove({}, (err) => {
  console.log('collection removed')
});

to call Model.remove with an empty object to remove all items in the collection that’s mapped to Model.

The callback is run when all the items are removed from the collection.

Conclusion

To drop a database with Mongoose, we can remove all the content of a collection.

Categories
JavaScript Answers

How to check if user is logged in with Passport.js?

Sometimes, we want to check if user is logged in with Passport.js.

In this article, we’ll look at how to check if user is logged in with Passport.js.

How to check if user is logged in with Passport.js?

To check if user is logged in with Passport.js, we can check is req.user is defined.

For instance, we write

if (req.user) {
  // logged in
} else {
  // not logged in
}

to do the check in a middleware function.

We write

const loggedIn = (req, res, next) => {
  if (req.user) {
    next();
  } else {
    res.redirect('/login');
  }
}

app.get('/me', loggedIn, (req, res, next) => {
  //...
});

to define the loggedIn middleware function with the if statement.

We call next to run the next middleware function if req.user is set and call res.redirect to redirect to the /login route otherwise.

And then we can use loggedIn in a route by passing it in as an argument.

Conclusion

To check if user is logged in with Passport.js, we can check is req.user is defined.

Categories
JavaScript Answers

How to append to new line in Node.js?

Sometimes, we want to append to new line in Node.js.

In this article, we’ll look at how to append to new line in Node.js.

How to append to new line in Node.js?

To append to new line in Node.js, we can use the write method with the os.EOL constant.

For instance, we write

const os = require("os");
const fs = require("fs");

fs.open('./log.txt', 'a', 666, (e, id) => {
  fs.write(id, text + os.EOL, null, 'utf8', () => {
    fs.close(id, () => {
      console.log('file is updated');
    });
  });
});

to call fs.open to open the ./log.txt file with 666 file permission and 'a' for append.

Then we call fs.write with the text appended with the os.EOL character, which is the new line character for the platform the app is running on.

Then we call fs.close to close the file handle given by id.