2017-10-08 49 views
0

我試圖使用節點和護照 這是架構單一的模式,但仍嵌套模式錯誤顯示

(型號/ 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) 

這是我的路由文件(路徑/ 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; 

我無法找到錯誤 它說你嵌套模式,以便利用裁判,但我只用單一架構 我試圖使用節點和護照 cameit 我想建立使用節點和護照 我app.js是完美的工作BU登錄和註冊系統,包括錯誤cameit說你嵌套模式之後建立登錄和註冊系統架構,以便使用,但裁判我只用過單個模式

回答

0

你有username的類型聲明上的拼寫錯誤。

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