Coder Social home page Coder Social logo

Comments (10)

bangundwi avatar bangundwi commented on August 19, 2024

Password

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on August 19, 2024

Sorry, that is not the answer we were looking for. The correct answer is: "email"

Leave a comment with the right answer to continue.😄

from intermediate-node-course.

bangundwi avatar bangundwi commented on August 19, 2024

email

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on August 19, 2024

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.

bangundwi avatar bangundwi commented on August 19, 2024

b. A MongoDB collection

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on August 19, 2024

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.

bangundwi avatar bangundwi commented on August 19, 2024

c. A Mongoose Model

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on August 19, 2024

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.

bangundwi avatar bangundwi commented on August 19, 2024

c

from intermediate-node-course.

github-learning-lab avatar github-learning-lab commented on August 19, 2024

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:

  1. Open a new console and type in the mongo command.
  2. Select our database: use userData
  3. Insert a new user into a users collection:db.users.insertOne({name:"octocat",email:"[email protected]",password:"password"})
  4. Find all users: db.users.find()
  5. 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.

Related Issues (2)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.