Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Vitaly Kashubin
Vitaly Kashubin
8,449 Points

I have "User.create is not a function" ERROR! Help plz

var mongoose = require('mongoose'); var Schema = mongoose.Schema;

var userSchema = new Schema({ email: { type:String, unique: true, required: true, trim: true }, name: { type: String, unique: true, required: true, trim: true }, favoriteBook: { type: String, required: true, trim: true }, password: { type: String, required: true }, confirmPassword: { type: String, required: true } }) var User = mongoose.model('User',userSchema); module.exports.User=User;

Vitaly Kashubin
Vitaly Kashubin
8,449 Points


const express = require('express'); const router = express.Router(); const User = require('../models/user');

//get /register router.get('/register',(req,res,next)=>{ return res.render('Register',{title:"Sign Up"}); res.clearCookie('email'); });

router.post('/register',(req,res,next)=>{ if(req.body.email && req.body.name && req.body.favoriteBook && req.body.password && req.body.confirmPassword ){ if(req.body.password !== req.body.confirmPassword){ let err = new Error('Passwords do not match'); err.status = 400; return next(err); }else{ // fll in req form to UserData const userData = { email: req.body.email, name: req.body.name, favoriteBook: req.body.favoriteBook, password: req.body.password }; //use schema`s 'create' method to insert document into Mongo User.create(userData,function(error,user){ if(error){ return next(error); }else{ req.session.userId = user._id; return res.redirect('/profile'); } }); } }else{ let err = new Error('All fields required'); err.status = 400; return next(err); } }); // GET / router.get('/', function(req, res, next) { return res.render('index', { title: 'Home' }); });

// GET /about router.get('/about', function(req, res, next) { return res.render('about', { title: 'About' }); });

// GET /contact router.get('/contact', function(req, res, next) { return res.render('contact', { title: 'Contact' }); });

module.exports = router;