Mongoose Schema in NodeJS TypeError: Schema is not a constructor
Mongoose Schema in NodeJS TypeError: Schema is not a constructor
我正在尝试为大学构建一个 MEAN Stack 应用程序,但我对这些东西还很陌生。我做了一个关于如何实现登录和注册的教程。目前,npm start 告诉我 New Schema 不是构造函数。我已经在寻找答案,但找不到答案。所以这是我的代码:
// imported libraries---------------------
var mongoose = require('mongoose');
var Schema = mongoose.schema;
var crypto = require('crypto');
var jwt = require('jsonwebtoken'); //generates our token which is required for
logging in
//Mongoose userScheme --------------------
var userSchema = new Schema({
email: {
type: String,
unique: true,
required: true
},
name: {
type: String,
required: true
},
//@arg hash: hashes provide security if the db is hacked --> password is
hashed
hash: String,
salt: String
});
/*
The following setPassword method takes the password and salt to create
a safe password and a salt which is then synchronized with the the DB
This method runs with every user login / registration
*/
userSchema.methods.setPassword = function(password){
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};
// checks if the entered password matches with the password stored in DB (hashed)
//returns a boolean
userSchema.methods.validPassword = function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash === hash;
};
userSchema.methods.generateJwt = function(){
var expiry = new Date();
expiry.setDate(expiry.getDate()+7); //token expires seven days after creation
return jwt.sign({
_id: this._id,
email: this.email,
name: this.name,
exp : parseInt(expiry.getTime()/1000),
}, "MY_SECRET"); //todo add this secret to mongo db
};
错误信息如下:
> groupup@1.0.0 start /Users/Dominik/IDE/ideaProjects/groupup
> node server.js
/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/users.js:20
var userSchema = new Schema({
^
TypeError: Schema is not a constructor
at Object.<anonymous> (/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/users.js:20:18)
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> (/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/db.js:53:1)
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)
我还检查了 mongoose 的文档,但对我来说,类型看起来还不错。
预先感谢您的帮助!
尝试对架构使用大写字母:
// v
var Schema = mongoose.Schema
而不是
var Schema = mongoose.schema;
我正在尝试为大学构建一个 MEAN Stack 应用程序,但我对这些东西还很陌生。我做了一个关于如何实现登录和注册的教程。目前,npm start 告诉我 New Schema 不是构造函数。我已经在寻找答案,但找不到答案。所以这是我的代码:
// imported libraries---------------------
var mongoose = require('mongoose');
var Schema = mongoose.schema;
var crypto = require('crypto');
var jwt = require('jsonwebtoken'); //generates our token which is required for
logging in
//Mongoose userScheme --------------------
var userSchema = new Schema({
email: {
type: String,
unique: true,
required: true
},
name: {
type: String,
required: true
},
//@arg hash: hashes provide security if the db is hacked --> password is
hashed
hash: String,
salt: String
});
/*
The following setPassword method takes the password and salt to create
a safe password and a salt which is then synchronized with the the DB
This method runs with every user login / registration
*/
userSchema.methods.setPassword = function(password){
this.salt = crypto.randomBytes(16).toString('hex');
this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};
// checks if the entered password matches with the password stored in DB (hashed)
//returns a boolean
userSchema.methods.validPassword = function(password){
var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
return this.hash === hash;
};
userSchema.methods.generateJwt = function(){
var expiry = new Date();
expiry.setDate(expiry.getDate()+7); //token expires seven days after creation
return jwt.sign({
_id: this._id,
email: this.email,
name: this.name,
exp : parseInt(expiry.getTime()/1000),
}, "MY_SECRET"); //todo add this secret to mongo db
};
错误信息如下:
> groupup@1.0.0 start /Users/Dominik/IDE/ideaProjects/groupup
> node server.js
/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/users.js:20
var userSchema = new Schema({
^
TypeError: Schema is not a constructor
at Object.<anonymous> (/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/users.js:20:18)
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> (/Users/Dominik/IDE/ideaProjects/groupup/SEBA_GroupUp/app_api/models/db.js:53:1)
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)
我还检查了 mongoose 的文档,但对我来说,类型看起来还不错。 预先感谢您的帮助!
尝试对架构使用大写字母:
// v
var Schema = mongoose.Schema
而不是
var Schema = mongoose.schema;