Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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;