Mongoose验证在heroku上崩溃

时间:2016-07-27 22:08:26

标签: node.js mongodb mongoose

您好我正在使用mongoose从mongodb获取数据。我在heroku上部署了应用程序。当我在本地运行我的应用程序时,如果用户存在,我会收到验证错误。如果我在heroku上运行它我得到验证错误但应用程序也崩溃了。

这是我创建用户的帖子请求。

    exports.create = function (req, res, next) {
      var newUser = new User(req.body);
      console.log(newUser);
      newUser.provider = 'local';
      newUser.role = 'user';
      newUser.save(function(err, user) {
        if (err) return res.status(400).json(err);
        var token = jwt.sign({_id: user._id }, config.secrets.session, { expiresIn: 60*5 });
        res.json({ token: token });
      });
    };     

这是猫鼬验证。

  UserSchema
    .path('email')
    .validate(function(value, respond) {
      var self = this;
      this.constructor.findOne({email: value}, function(err, user) {
        if(err) throw err;
        if(user) {
          if(self.id === user.id) return respond(true);
          return respond(false);
        }
        respond(true);
      });
  }, 'The specified email address is already in use.');

这是错误:

“stack”:“错误\ n在MongooseError.ValidationError(/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/error/validation.js:22:16)\ n在model.Document.invalidate( /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/document.js:1162:32)\n at /Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/document.js:1037:16\n在验证(/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/schematype.js:653:7),在/Users/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/lib/schematype.js:678: 11 \ n在Query。(/ Users/panayiotis/WebstormProjects/SmartHome/server/api/user/user.model.js:80:16)。在/ Users / panayiotis / WebstormProjects / SmartHome / node_modules / mongoose / node_modules / kareem / index.js:177:19 \ n atUsers/panayiotis/WebstormProjects/SmartHome/node_modules/mongoose/node_modules/kareem/index.js:109:16,n at nextTickCallbackWith0Args(node.js:420:9)\ n在process._tickCallback(node.js :349:13)”,     “消息”:“用户验证失败”,     “name”:“ValidationError”,     “错误”:{         “电子邮件”:{             “properties”:{                 “type”:“用户定义”,                 “message”:“指定的电子邮件地址已被使用。”,                 “路径”:“电子邮件”,                 “价值”:“test1@test.com”             },

1 个答案:

答案 0 :(得分:0)

我解决了更新猫鼬的问题