Categories
JavaScript Answers

How to delete array element in document and save with Mongoose?

Sometimes, we want to delete array element in document and save with Mongoose.

In this article, we’ll look at how to delete array element in document and save with Mongoose.

How to delete array element in document and save with Mongoose?

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.

For instance, we write

Favorite.updateOne({
  name
}, {
  $pullAll: {
    favorites: req.params.deleteUid,
  },
});

to call updateOne with an object to search for the document with the name value set to name.

The 2nd object has the $pullAll property which lets us remove the favorites entries in the selected Favorite document with the _id set to req.params.deleteUid.

Conclusion

To delete array element in document and save with Mongoose, we can use the updateOne method with the $pullAll operator.

Categories
JavaScript Answers

How to determine whether 64 bit or 32 bit Node executable installed?

Sometimes, we want to determine whether 64 bit or 32 bit Node executable installed.

In this article, we’ll look at how to determine whether 64 bit or 32 bit Node executable installed.

How to determine whether 64 bit or 32 bit Node executable installed?

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

For instance, we run

node -p "process.arch"

to print the value of the process.arch property into the console.

The possible values for process.arch includes

'arm', 'arm64' 'ia32' 'mips' 'mipsel' 'ppc' 'ppc64' 's390' 's390x' 'x32' 'x64'

Conclusion

To determine whether 64 bit or 32 bit Node executable installed, we use the process.arch property.

Categories
JavaScript Answers

How to fix the ‘Cannot find module’ when importing components with absolute paths with Jest?

To fix the ‘Cannot find module’ when importing components with absolute paths with Jest, we can set the roots setting of the Jest config.

For instance, we write

"jest": {
  "roots": [
    "<rootDir>",
    "/home/some/path/"
  ],
  "modulePaths": [
    "<rootDir>",
    "/home/some/other/path"
  ],
  "moduleDirectories": [
    "node_modules"
  ],
}

to add the '<rootDir>' and "/home/some/path/" path strings into the roots array.

This makes these 2 paths the root path when we use absolute imports.

Likewise, we add the same entries to modulePaths to add them as root paths for absolute imports.

Categories
JavaScript Answers

How to find user by username LIKE value with Mongoose.js?

Sometimes, we want to find user by username LIKE value with Mongoose.js.

In this article, we’ll look at how to find user by username LIKE value with Mongoose.js.

How to find user by username LIKE value with Mongoose.js?

To find user by username LIKE value with Mongoose.js, we can use findOne with a regex.

For instance, we write

const name = 'Peter';

model.findOne({
  name: new RegExp(`^${name}$`, "i")
}, (err, doc) => {
  //...
});

to call model.findOne with an object that searches the name field with a regex.

We set name to a regex that matches anything that has name in the string.

And we add the 'i' flag to the regex to search in a case-insensitive manner.

As a result, any entry with name having 'Peter' anywhere in the string would be matched.

Conclusion

To find user by username LIKE value with Mongoose.js, we can use findOne with a regex.

Categories
JavaScript Answers

How to pass additional form fields to the local authentication strategy Using Node.js Passport.js?

Sometimes, we want to pass additional form fields to the local authentication strategy Using Node.js Passport.js.

In this article, we’ll look at how to pass additional form fields to the local authentication strategy Using Node.js Passport.js.

How to pass additional form fields to the local authentication strategy Using Node.js Passport.js?

To pass additional form fields to the local authentication strategy Using Node.js Passport.js, we set the passReqToCallback option to true.

For instance, we write

passport.use(new LocalStrategy({
    usernameField: 'email',
    passReqToCallback: true
  },
  (req, email, password, done) =>{
    // ...
  }
));

to create a LocalStrategy instance with an object that has passReqToCallback set to true.

As a result, the middleware function in the 2nd argument would have the full req object from other middlewares before it passed into it.

Conclusion

To pass additional form fields to the local authentication strategy Using Node.js Passport.js, we set the passReqToCallback option to true.