单个模式但仍显示嵌套模式错误

时间:2017-10-08 10:47:34

标签: angularjs node.js passport.js

我正在尝试使用节点和护照构建登录和注册系统 这是架构

(型号/ user.js的)

var mongoose = require('mongoose');  
var bcrypt = require('bcryptjs');

//User Schema
var UserSchema = mongoose.Schema({
    username: {
        tye: String,
        index: true
    },
    password: {
        type: String
    },
    email: {
        type: String
    },
    name: {
        type: String
    }
});

var User = module.exports = mongoose.model('User', UserSchema);

module.exports.createUser = function(newUser, callback){
    bcrypt.genSalt(10, function(err, salt) {
    bcrypt.hash(newUser.password, salt, function(err, hash) {
        newUser.password = hash;
        newUser.save(callback);
    });
});
}

我只使用了单个架构UserSchema

错误是

C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:646
    throw new TypeError('Undefined type `' + name + '` at `' + path +
    ^

TypeError: Undefined type `undefined` at `username.index`
  Did you try nesting Schemas? You can only nest using refs or arrays.
    at Function.Schema.interpretAsType (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:646:11)
    at Schema.path (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:502:29)
    at Schema.add (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:383:12)
    at Schema.add (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:372:14)
    at new Schema (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:104:10)
    at Mongoose.Schema (C:\xampp\htdocs\loginapp\node_modules\mongoose\lib\schema.js:75:12)
    at Object.<anonymous> (C:\xampp\htdocs\loginapp\models\user.js:5:27)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (C:\xampp\htdocs\loginapp\routes\users.js:4:12)
    at Module._compile (module.js:570:32)

这是我的路由文件(routes / user.js)

var express = require('express');
var router = express.Router();

var user = require('../models/user');

//Register
router.get('/register', function(req,res){
    res.render('register');
});

//Login
router.get('/login', function(req,res){
    res.render('login');
});

//Register User
router.post('/register', function(req,res){
   var name = req.body.name;
    var email = req.body.email;
    var username = req.body.username;
    var password = req.body.password;
    var password2 = req.body.password2;

    //Validation
    req.checkBody('name', 'Name is required').notEmpty();
    req.checkBody('email', 'Email is required').notEmpty();
    req.checkBody('email', 'Email is not valid').isEmail();
    req.checkBody('username', 'Username is required').notEmpty();
    req.checkBody('password', 'Password is required').notEmpty();
    req.checkBody('password2', 'Password do not match').equals(req.body.password);

    var errors = req.validationErrors();
    if(errors){
       res.render('register', {
           errors: errors
       });
    }
    else{
        var newUser = new User({
            name: name,
            email: email,
            username: username,
            password: password
        });
        User.createUser(newUser, function(err, user){
            if(err) throw err;
            console.log(user);
        });

        req.flash('success_msg', 'You are registered and can now login');
        res.redirect('/users/login'); 
    }
});


module.exports = router;

我无法找到错误 它说你有嵌套模式所以使用refs但我只使用了单一模式 我正在尝试使用节点和护照构建登录和注册系统  cameit 我正在尝试使用节点和护照构建登录和注册系统 我的app.js完全正常工作包括模式后错误来说你有嵌套模式所以使用refs但我只使用单一模式

1 个答案:

答案 0 :(得分:0)

您在username的类型声明上有拼写错误。

username: {
  tye: String, // typo here, should read 'type'
  index: true
},