• 10 hours
  • Medium

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 4/28/21

Save and retrieve data

Log in or subscribe for free to enjoy all this course has to offer!

Using the Thing model we created in the previous chapter, we are going to leverage Mongoose to make saving and retrieving data to and from the database a walk in the park.  Let's start by properly implementing our POST route.

Saving Things to the database

To be able to use our new Mongoose model in our app, we'll need to import it in the  app.js  file:

const Thing = require('./models/thing');

Now replace the logic in your POST route with the following:

app.post('/api/stuff', (req, res, next) => {
const thing = new Thing({
title: req.body.title,
description: req.body.description,
imageUrl: req.body.imageUrl,
price: req.body.price,
userId: req.body.userId
});
thing.save().then(
() => {
res.status(201).json({
message: 'Post saved successfully!'
});
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
});

Here, create a new instance of your  Thing  model, passing it a JavaScript object containing all of the information it needs from the parsed request body.  That model has a  save()  method which simply saves your Thing to the database.

The  save()  method returns a promise, so in our  then()  block, we send back a success response, and in our  catch()  block, we send back an error response with the error thrown by Mongoose.

Retrieving the list of Things for sale

Now we can implement our GET route to return all of the Things in the database:

app.use('/api/stuff', (req, res, next) => {
Thing.find().then(
(things) => {
res.status(200).json(things);
}
).catch(
(error) => {
res.status(400).json({
error: error
});
}
);
});

Here, we use the  find()  method on our Mongoose model to return an array containing all of the  Things  in our database. Now, if you add a new  Thing , it should appear immediately in your Stuff for Sale page.

However, if you click on one of the Things, the single item view does not work, as it is trying to make a different GET call to find an individual thing.  Let's implement that route now.

Retrieving a specific Thing

Let's add another route to our app, just after our POST route:

app.get('/api/stuff/:id', (req, res, next) => {
Thing.findOne({
_id: req.params.id
}).then(
(thing) => {
res.status(200).json(thing);
}
).catch(
(error) => {
res.status(404).json({
error: error
});
}
);
});

In this route:

  • we use the  get()  method to only react to GET requests to this endpoint

  • we use a colon in front of the dynamic segment of the route to make it accessible as a parameter

  • we then use the  findOne()  method on our  Thing  model to find the single  Thing  with the same  _id  as the request parameter

  • that  Thing  is then returned in a promise, and sent to the front end

  • if no  Thing  is found or an error occurs, we send a 404 error to the front end, along with the thrown error

Our app is really starting to take shape now. In the next chapter, we will implement our Modify and Delete buttons to complete the  Thing  part of our API.

Example of certificate of achievement
Example of certificate of achievement