S
S
Sergey Beloventsev2016-08-27 12:32:52
Node.js
Sergey Beloventsev, 2016-08-27 12:32:52

What causes the ReferenceError: Schema is not defined error?

var crypto = require("crypto");
var mongoose = require("../libs/mongoose");

var schema = new Schema({
    username:{
        type:Stryng,
        unique:true,
        required:true
    },
    hashedPassword:{
        type:String,
        required:true
    },
    salt:{
        type:String,
        required:true
    },
    created:{
        type:Date,
        default:Date.now
    }
});
schema.metods.encryptPassword = function(password){
    return crypto.createHmac("sha1",this.salt).update(password).digest('hex');
};

schema.virtual('password')
    .set(function(password){
    this._plainPassword = password;
    this.salt = Math.random();
    this.hashedPassword = this.encryptPassword(password);
    })
    .get(function(){return this._plainPassword});
shema.methods.checkPassword = function (password) {
    return this.encryptPassword(password) === this.hashedPassword;
};
exports.User = mongoose.model('User',schema);

this is the error why?
/var/www/html/chatnode/models/user.js:4
var schema = new Schema({
                 ^

ReferenceError: Schema is not defined
    at Object.<anonymous> (/var/www/html/chatnode/models/user.js:4:18)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Module.require (module.js:466:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/var/www/html/chatnode/createDb.js:1:74)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:159:18)

here is the error

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2016-08-27
@Sergalas

Because you don't have a Schema variable defined,
add to the beginning of the file: var Schema = mongoose.Schema
ps Well, just in case, let's figure it out.
in translation - Schema - not defined. And indeed, if you look at your code, you don't have a style entry anywhere var Schema = ..., so you can't use this variable because it simply doesn't exist. It doesn't require it in your file, you don't define it in any way...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question