Guest User

Untitled

a guest
Feb 25th, 2018
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. TypeError: Cannot read property 'name' of undefined
  2. at router.post (/Users/jonathanschroeder/Desktop/authapp/routes/users.js:12:24)
  3. at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
  4. at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:137:13)
  5. at Route.dispatch (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/route.js:112:3)
  6. at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
  7. at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:281:22
  8. at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
  9. at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
  10. at Function.handle (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:174:3)
  11. at router (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:47:12)
  12. at Layer.handle [as handle_request] (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/layer.js:95:5)
  13. at trim_prefix (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:317:13)
  14. at /Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:284:7
  15. at Function.process_params (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:335:12)
  16. at next (/Users/jonathanschroeder/Desktop/authapp/node_modules/express/lib/router/index.js:275:10)
  17. at serveStatic (/Users/jonathanschroeder/Desktop/authapp/node_modules/serve-static/index.js:75:16)
  18.  
  19. const mongoose = require('mongoose');
  20. //bcrpypt for encrpyption
  21. const bcrypt = require('bcryptjs');
  22. //to connect to database
  23. const config = require('../config/database');
  24.  
  25.  
  26. //Create the Schema
  27. const UserSchema = mongoose.Schema({
  28. name: {
  29. type: String
  30. },
  31. email: {
  32. type: String,
  33. require: true,
  34. unique: true //no two users can share the same email address.
  35. },
  36. username: {
  37. type: String,
  38. require: true
  39. },
  40. password: {
  41. type: String,
  42. require: true
  43. }
  44. });
  45. //Create variable that can be used outside. set that to mongoose model and pass in the UserSchema
  46. const User = module.exports = mongoose.model('User', UserSchema);
  47.  
  48. //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.
  49. module.exports.getUserById = function(id, callback){
  50. User.findById(id, callback);
  51. }
  52.  
  53. module.exports.getUserByUsername = function(username, callback){
  54. const query = {username:username} //findOne function takes in query
  55. User.findOne(query, callback);
  56. }
  57. //pass in 10 which means Number of rounds to use. (Default is 10 anyways but I will pass 10)
  58. //reference in case I forget www.npmjs.com/package/bcryptjs
  59. //so it is confusing but this returns the hashed password.
  60. //https://busy.org/@nafestw/mean-tutorial-part-2-adding-a-user-model was a great tutorial for this.
  61. 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.
  62. bcrypt.genSalt(10, (err, salt) => {
  63. bcrypt.hash(newUser.password, salt, (err, hash) => {
  64. if(err) throw err;
  65. newUser.password = hash;
  66. newUser.save(callback);
  67. });
  68. });
  69. }
  70.  
  71. const express = require('express');
  72. const router = express.Router();
  73. const passport = require('passport');
  74. const jwt = require('jsonwebtoken');
  75. //Now that I created the model I will bring it in here.
  76. const User = require('../models/user');
  77.  
  78. //Registration
  79. router.post('/register', (req,res,next) =>{
  80. //res.send('registration');
  81. let newUser = new User({
  82. name: req.body.name,
  83. email: req.body.email,
  84. username: req.body.username,
  85. password: req.body.password //I will run this password through bcrypt.hash which will has before db.
  86. });
  87. User.addUser(newUser, (err, user) =>{ //I will create this addUser function inside the models user.js
  88. if(err){
  89. res.json({success:false, msg:'Registration Failed!'})
  90. }else{
  91. res.json({success:true, msg:'User is Registered!'})
  92. }
  93. });
  94. });
  95.  
  96.  
  97.  
  98.  
  99. module.exports = router;
  100.  
  101. const express = require('express');
  102. const path = require('path');
  103. const bodyParser = require('body-parser');
  104. const cors = require('cors');
  105. const passport = require('passport');
  106. const mongoose = require('mongoose');
  107.  
  108. const config = require('./config/database')
  109.  
  110.  
  111. mongoose.connect(config.database);
  112.  
  113.  
  114. mongoose.connection.on('connected',function(){console.log('yay i am connected to database'+config.database)});
  115.  
  116.  
  117. mongoose.connection.on('error',function(error){console.log('You have an error'+error)});
  118.  
  119.  
  120.  
  121. const app = express();
  122.  
  123.  
  124. const users = require('./routes/users');
  125.  
  126.  
  127.  
  128. const port = 3000;
  129.  
  130.  
  131. app.use(cors());
  132.  
  133.  
  134. app.use(express.static(path.join(__dirname, 'public')))
  135.  
  136.  
  137. app.get('/', function(req,res){res.send('Sending Response')})
  138.  
  139.  
  140. app.use('/users', users)
  141.  
  142.  
  143. app.listen(port, function(){console.log('Server started on port '+port)})
  144.  
  145.  
  146. app.use(bodyParser.json());
  147.  
  148. {
  149. "name":"John Doe",
  150. "email":"john@doe.com",
  151. "username":"johndoe",
  152. "password":"123456"
  153. }
  154.  
  155. module.exports = {
  156. database:'mongodb://localhost:27017/authapp',
  157. secret:'NintamaRantaro'
  158. }
Add Comment
Please, Sign In to add comment