Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TypeError: Cannot read property 'name' of undefined
- at router.post (/Users/jonathanschroeder/Desktop/authapp/routes/users.js:12:24)
- at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
- at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:137:13)
- at Route.dispatch (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:112:3)
- at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
- at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:281:22
- at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
- at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
- at Function.handle (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:174:3)
- at router (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:47:12)
- at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
- at trim_prefix (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:317:13)
- at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:284:7
- at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
- at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
- at serveStatic (/Users/jonathanschroeder/Desktop/authapp/node_modules/serve-static/index.js:75:16)
- const mongoose = require('mongoose');
- //bcrpypt for encrpyption
- const bcrypt = require('bcryptjs');
- //to connect to database
- const config = require('../config/database');
- //Create the Schema
- const UserSchema = mongoose.Schema({
- name: {
- type: String
- },
- email: {
- type: String,
- require: true,
- unique: true //no two users can share the same email address.
- },
- username: {
- type: String,
- require: true
- },
- password: {
- type: String,
- require: true
- }
- });
- //Create variable that can be used outside. set that to mongoose model and pass in the UserSchema
- const User = module.exports = mongoose.model('User', UserSchema);
- //Create two functions. Get the user by ID and get the user by username. Of course I need to do module.exports to use it outside.
- module.exports.getUserById = function(id, callback){
- User.findById(id, callback);
- }
- module.exports.getUserByUsername = function(username, callback){
- const query = {username:username} //findOne function takes in query
- User.findOne(query, callback);
- }
- //pass in 10 which means Number of rounds to use. (Default is 10 anyways but I will pass 10)
- //reference in case I forget www.npmjs.com/package/bcryptjs
- //so it is confusing but this returns the hashed password.
- //https://busy.org/@nafestw/mean-tutorial-part-2-adding-a-user-model was a great tutorial for this.
- module.exports.addUser = function(newUser, callback){ //I did the callback in the route user.js where it gives success or fail and res.sends success or fail.
- bcrypt.genSalt(10, (err, salt) => {
- bcrypt.hash(newUser.password, salt, (err, hash) => {
- if(err) throw err;
- newUser.password = hash;
- newUser.save(callback);
- });
- });
- }
- const express = require('express');
- const router = express.Router();
- const passport = require('passport');
- const jwt = require('jsonwebtoken');
- //Now that I created the model I will bring it in here.
- const User = require('../models/user');
- //Registration
- router.post('/register', (req,res,next) =>{
- //res.send('registration');
- let newUser = new User({
- name: req.body.name,
- email: req.body.email,
- username: req.body.username,
- password: req.body.password //I will run this password through bcrypt.hash which will has before db.
- });
- User.addUser(newUser, (err, user) =>{ //I will create this addUser function inside the models user.js
- if(err){
- res.json({success:false, msg:'Registration Failed!'})
- }else{
- res.json({success:true, msg:'User is Registered!'})
- }
- });
- });
- module.exports = router;
- const express = require('express');
- const path = require('path');
- const bodyParser = require('body-parser');
- const cors = require('cors');
- const passport = require('passport');
- const mongoose = require('mongoose');
- const config = require('./config/database')
- mongoose.connect(config.database);
- mongoose.connection.on('connected',function(){console.log('yay i am connected to database'+config.database)});
- mongoose.connection.on('error',function(error){console.log('You have an error'+error)});
- const app = express();
- const users = require('./routes/users');
- const port = 3000;
- app.use(cors());
- app.use(express.static(path.join(__dirname, 'public')))
- app.get('/', function(req,res){res.send('Sending Response')})
- app.use('/users', users)
- app.listen(port, function(){console.log('Server started on port '+port)})
- app.use(bodyParser.json());
- {
- "name":"John Doe",
- "email":"john@doe.com",
- "username":"johndoe",
- "password":"123456"
- }
- module.exports = {
- database:'mongodb://localhost:27017/authapp',
- secret:'NintamaRantaro'
- }
Add Comment
Please, Sign In to add comment