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.

Categories
JavaScript Answers

How to remove object from array with MongoDB?

Sometimes, we want to remove object from array with MongoDB.

In this article, we’ll look at how to remove object from array with MongoDB.

How to remove object from array with MongoDB?

To remove object from array with MongoDB, we can use the $pull operator.

For instance, we write

db.mycollection.update({
    '_id': ObjectId("5150a1199fac0e6910000002")
  }, {
    $pull: {
      items: {
        id: 23
      }
    }
  },
  false,
  true,
);

to call update with an object that has the $pull property with items.id set to 23 to remove the items entry with id 23.

We pass in false to disable upsert and pass in true to let us update multiple entries.

Conclusion

To remove object from array with MongoDB, we can use the $pull operator.

Categories
JavaScript Answers

How to respond with a JSON object in Node.js and Express?

Sometimes, we want to respond with a JSON object in Node.js and Express.

In this article, we’ll look at how to respond with a JSON object in Node.js and Express.

How to respond with a JSON object in Node.js and Express?

To respond with a JSON object in Node.js and Express, we can call the res.json method.

For instance, we add

res.json({
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
});

in our route handler function to respond with

{
  anObject: {
    item1: "item1val",
    item2: "item2val"
  },
  anArray: ["item1", "item2"],
  another: "item"
}

res.json takes any plain JavaScript object and return it as a JSON string response.

Conclusion

To respond with a JSON object in Node.js and Express, we can call the res.json method.