node.js - NodeJS 中的 Mongoose 模式类型错误 : Schema is not a constructor

标签 node.js typeerror 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;

关于node.js - NodeJS 中的 Mongoose 模式类型错误 : Schema is not a constructor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44481941/

相关文章:

node.js - Electron/Angular2/打字应用程序 : Import module in a component

node.js - 如何使用node-nlp从句子中提取变量

javascript - 未捕获的类型错误 : Object has no method 'contains'

node.js - 类型错误:无法读取未定义的属性 'getAppPath'

node.js - Mongoose 需要一个基于另一个字段的一些枚举值的字段

javascript - 类型错误 : (fn) is not iterable when just executing a function

node.js - Laravel Mix : Update a Node. js 依赖

python - 类型错误:列表索引必须是整数,而不是 unicode(Telepot 检索名称)

javascript - Express JS mongoose model.find 在 es6 中未定义

javascript - 如何使用 ejs post 表单更新 node.js 和 MongoDB 应用程序中的现有用户数据?