S
S
Sergey Beloventsev2016-08-27 11:58:28
Node.js
Sergey Beloventsev, 2016-08-27 11:58:28

Why is the Cannot find module '/models/user' error thrown?

actually here at this address models/user is the module user.js here is the code

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);

like this I'm trying to use at the root of the main
var User = require("models/user").User;

var user = new User({
    username:"Tester",
    password:"test"
});

user.save(function(err,user,affected){
    if(err) throw err;
});

actually here is the error
module.js:440
    throw err;
    ^

Error: Cannot find module '/models/user'
    at Function.Module._resolveFilename (module.js:438:15)
    at Function.Module._load (module.js:386:25)
    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)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-08-27
@Sergalas

var User = require("./models/user").User;
Read about how the module system works in Node (or this ).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question