2016-04-13 2 views
2

Использование NodeJS (v0.10.25) и ExpressJS (v4.4.12), а также следующие схемыОшибка при запуске приложения с mongooseSchema.methods функций

var mongoose  = require('mongoose'); 
var Schema  = mongoose.Schema; 
var models  = require('./inherit/models.js'); 

var userSchema = new Schema({ 
    username: { type: String, required: true, unique: true, index: true }, 
    password: { type: String, required: true , minlength: 128, maxlength: 128 }, 
    email: { type: String, required: true, unique: true, index: true }, 
    profileImage: { type: String }, 
    company: { type: Schema.Types.ObjectId, ref: 'Company' }, 
    info: { 
     name: { type: String, required: true }, 
     dateOfBirth: { type: Date }, 
     gender: { type: String, enum: ['M', 'F'] } 
    }, 
    address: [ models.addressModel ], 
    contacts: [ models.contactModel ], 
    flags: [ models.flagsModel ], 
    login: { 
     lastDate: { type: Date }, 
     lastIp: { type: String }, 
     dtCreate: { type: Date }, 
     dtUpdate: { type: Date } 
    } 
}); 

userSchema.methods.findComplete = function(findQuery, populateObject) { 
    if (typeof populateObject === 'undefined') populateObject = 'company'; 

    return this.model('User').find(findQuery).populate(populate).exec(); 
}; 

userSchema.methods.login = function(username, password) { 
    //Some logic 

    return true; 
} 

userSchema.methods.contactTypes = function() { 
    return { 
     1: 'E-mail adicional', 
     2: 'Telefone residencial', 
     3: 'Telefone comercial', 
     4: 'FAX', 
     50: 'Telefone celular', 
     51: 'Celular (VIVO)', 
     52: 'Celular (TIM)', 
     53: 'Celular (Claro)', 
     54: 'Celular (Oi)', 
     55: 'Celular(Nextel)' 
    } 
} 

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

При попытке запуска приложения я получаю следующее сообщение об ошибке :

TypeError: Cannot read property 'scope' of undefined 
    at model.Object.defineProperty.set [as login] (/home/grupow/Projetos/mean/node_modules/mongoose/lib/document.js:1638:25) 
    at applyMethods (/home/grupow/Projetos/mean/node_modules/mongoose/lib/model.js:3151:31) 
    at Function.compile (/home/grupow/Projetos/mean/node_modules/mongoose/lib/model.js:3121:3) 
    at Mongoose.model (/home/grupow/Projetos/mean/node_modules/mongoose/lib/index.js:392:17) 
    at Object.<anonymous> (/home/grupow/Projetos/mean/models/users.js:54:27) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
    at Module.require (module.js:364:17) 
    at require (module.js:380:17) 
    at Object.<anonymous> (/home/grupow/Projetos/mean/routes/users.js:5:16) 
    at Module._compile (module.js:456:26) 
    at Object.Module._extensions..js (module.js:474:10) 
    at Module.load (module.js:356:32) 
    at Function.Module._load (module.js:312:12) 
[nodemon] app crashed - waiting for file changes before starting... 

Когда «userSchema.methods» удалены, приложение запуска, в противном случае, эта ошибка будет всегда отображаться.

Не могли бы вы помочь мне?

ответ

1

Вы не можете назвать метод тем же именем, что и свойство, то же самое относится к любому объекту. Попробуйте изменить имя метода на setLogin или что-то еще.

Mongoose имеет закрытый вопрос, который объяснил, что я сказал here

+0

Спасибо, что работал как шарм. –