【问题标题】:Mongoose schema constructor not recognizedMongoose 架构构造函数无法识别
【发布时间】:2017-03-16 11:59:42
【问题描述】:

我有一个使用 Mongoose 的节点服务器,带有一个用户模式。我正在尝试编写注册函数,但出现以下错误:

TypeError:这不是构造函数

这发生在这段代码中:

var mongoose = require('mongoose');
var passwordHasher = require('password-hash-and-salt');

var User = new mongoose.Schema({
  username: String,
  hashedPassword: String,
  email: String
});

User.statics.signup = function(un, pw, em, cb){
  this.findOne( { $or:[ {'username': un}, {'email': em} ] }, function(err, person){
    if (err) cb(false, "Server error");
    else if (person == null){
      passwordHasher(pw).hash(function(error, hash){
        console.log(un, pw, em);
        if (error) cb(false, "Server error");
        var newUser = new User({ username: un, hashedPassword: hash, email: em});
                      ^ (location of error)
        newUser.save(function(err){
          if (err) cb(false, "Server error");
          else {
            cb(true, null);
          }
        });
      });
    }
    else if (person.email == em && person.username == un) cb(false, "Username and email are taken");
    else if (person.email == em) cb(false, "Email is taken");
    else if (person.username == un) cb(false, "Username is taken");
  });
}

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

我对 Javascript 不是很熟悉,所以这可能是我在方法的其他地方漏掉的一个简单的错字。但是,我确实研究了任何我能找到的类似错误的问题,建议的更改并没有解决问题。

【问题讨论】:

    标签: javascript node.js mongodb express typeerror


    【解决方案1】:

    我认为您不能使用架构保存新对象。试试这个方法:

    var mongoose = require('mongoose');
    var passwordHasher = require('password-hash-and-salt');
    
    var User = new mongoose.Schema({
      username: String,
      hashedPassword: String,
      email: String
    });
    
    var model = mongoose.model('User', User);
    module.exports = model;
    
    
    module.exports.statics.signup = function(un, pw, em, cb){
      model.findOne( { $or:[ {'username': un}, {'email': em} ] }, function(err, person){
        if (err) cb(false, "Server error");
        else if (person == null){
          passwordHasher(pw).hash(function(error, hash){
            console.log(un, pw, em);
            if (error) cb(false, "Server error");
            var newUser = new model({ username: un, hashedPassword: hash, email: em});
                          ^ (change this)
            newUser.save(function(err){
              if (err) cb(false, "Server error");
              else {
                cb(true, null);
              }
            });
          });
        }
        else if (person.email == em && person.username == un) cb(false, "Username and email are taken");
        else if (person.email == em) cb(false, "Email is taken");
        else if (person.username == un) cb(false, "Username is taken");
      });
    }
    

    希望这项工作

    编辑:请确保 model.statics 存在,否则会引发错误 或者您可以跳过静态并直接分配函数,如下所示:

    module.exports.signup = function(...
    

    【讨论】:

    • 这是给我TypeError: Cannot set property 'signup' of undefined,在线module.exports.statics.signup = ...
    • 那是因为你的 module.exports.statics 是“未定义的”。
    • 你必须确保你的 model.statics 存在,否则你可以这样做:module.exports.signup = function(...
    • 就是这样,谢谢!如果您想编辑您的帖子以提及静态在顶部未定义,那可能会帮助其他正在寻找的人。无论哪种方式,我都会标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 2021-07-23
    • 2016-02-02
    • 2018-06-16
    相关资源
    最近更新 更多