Comments (7)
from intermediate-node-course.
Correct! In this case, emails need to be unique. It makes sense to have at least one unique value, to avoid errors with authentication later on.
Here we are using route chaining as a shorthand for the "get", "put", and "delete" routes, since they all use the '/users/:id' endpoint. Remember that in 'users/:id' endpoint, id is a variable which can be accessed in the "req.params".
Now that we have a model, let's import it and connect to our database. Go to your server.js file and add these lines after all the libraries are imported.
const User=require('./models/User');
mongoose.connect('mongodb://localhost/userData')
The first line makes our User model available to use in express routes. The second line connects us to a local mongoDB database called: userData.
In the server.js file, you will notice some express routes set up for our users. It should look like this:
// CREATE
app.post('/users',(req,res)=>{
// User.create()
})
app.route('/users/:id')
// READ
.get((req,res)=>{
// User.findById()
})
// UPDATE
.put((req,res)=>{
// User.findByIdAndUpdate()
})
// DELETE
.delete((req,res)=>{
// User.findByIdAndDelete()
})
Inside each (req,res) callback function we use mongoose methods on our User model to Create, Read, Update, and Delete individual user documents in our users collection. The "POST" route is different than the others because mongoDB automatically creates an ID for each document when it is created. We are using route chaining as a shorthand for the "GET", "PUT", and "DELETE" routes, since they all use the /users/:id
endpoint. The :id
part of the endpoint is a variable which can be accessed in the "req.params".
In the commented out code in the routes above, what does User represent?
a. A MongoDB document
b. A MongoDB collection
c. A Mongoose Model
d. Data sent in the request body
from intermediate-node-course.
a
from intermediate-node-course.
Sorry, that is not the answer we were looking for. The correct answer is: "c"
Leave a comment with the right answer to continue.😄
from intermediate-node-course.
c
from intermediate-node-course.
You got it! Mongoose models come with a lot of useful methods (such as create) that make it easy to query for user documents in our user collection.
You can use the mongo shell to visualize your database and manipulate it through the command line. Before setting up our express routes with mongoose, try doing the following mongo shell commands:
- Open a new console and type in the
mongo
command. - Select our database:
use userData
- Insert a new user into a users collection:
db.users.insertOne({name:"octocat",email:"[email protected]",password:"password"})
- Find all users:
db.users.find()
- Delete all users:
db.users.drop()
This is a good place for a commit. If a user was successfully saved to the database, push your code to GitHub to continue:
git add .
git commit -m"add user model and connect to db"
git push origin master
from intermediate-node-course.
Awesome! 👏 The mongo shell is good for debugging, but from here on we will be using express routes to Create, Read, Update, and Delete user data.
Click here to get started.
from intermediate-node-course.
Related Issues (4)
- Set up node project HOT 3
- Create, Read, Update, and Delete HOT 5
- Refactor HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from intermediate-node-course.