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.

Categories
JavaScript Answers

How to return data from Axios API with Node.js?

Sometimes, we want to return data from Axios API with Node.js

In this article, we’ll look at how to return data from Axios API with Node.js.

How to return data from Axios API with Node.js?

To return data from Axios API with Node.js, we can return the properties from the data property.

For instance, we write

const axiosTest = async (url) => {
  try {
    const {
      data: response
    } = await axios.get(url)
    return response
  } catch (error) {
    console.log(error);
  }
}

to call axios.get with the url string.

It returns a promise and we get the resolve value of the promise with await.

And then we get the data property to get the response body object and we return it after assigning to response.

We use a catch block to catch any errors.

Conclusion

To return data from Axios API with Node.js, we can return the properties from the data property.

Categories
JavaScript Answers

How to perform a full text search in Node.js MongoDB and Mongoose?

Sometimes, we want to perform a full text search in Node.js MongoDB and Mongoose.

In this article, we’ll look at how to perform a full text search in Node.js MongoDB and Mongoose.

How to perform a full text search in Node.js MongoDB and Mongoose?

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.

For instance, we write

const schema = new Schema({
  name: String,
  email: String,
  profile: {
    something: String,
    somethingElse: String
  }
});

schema.index({
  name: 'text',
  'profile.something': 'text'
});

to call schema.index method to create text indexes on the name and profile.something fields.

As a result, we can do full text search on the name and profile.something fields.

Conclusion

To perform a full text search in Node.js MongoDB and Mongoose, we create an index on the schema.

Categories
JavaScript Answers

How to add a timestamp field into a Mongoose schema?

Sometimes, we want to add a timestamp field into a Mongoose schema.

In this article, we’ll look at how to add a timestamp field into a Mongoose schema.

How to add a timestamp field into a Mongoose schema?

To add a timestamp field into a Mongoose schema, we can use set the timestamps option when we create the schema.

For instance, we write

const mongoose = require('mongoose');
const {
  Schema
} = mongoose;

const schemaOptions = {
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at'
  },
};

const mySchema = new Schema({
  name: String
}, schemaOptions);

to create the mySchema schema with the Schema constructor.

We pass in schemaOptions as the 2nd argument, which has the timestamps option.

We set the createdAt and updatedAt properties to the field names of the created at and updated at timestamp fields.

Conclusion

To add a timestamp field into a Mongoose schema, we can use set the timestamps option when we create the schema.