sails.js“1属性无效”“代码”:“E_VALIDATION”,“invalidAttributes”

时间:2018-03-25 13:09:23

标签: javascript node.js sails.js waterline

我对sails.js框架有些麻烦。我对这个水线和sails.js框架的invalidattributes有一些问题。怎么试着这个来解决它。但没有变化..我不知道问题在哪里......而且它崩溃了...这个脚本中的错误是什么?是一个人在这里可以告诉我并向我解释我能做什么,这就是水线的这个令人毛骨悚然的错误可以阻止它。在sails.js中没有支持 - 但是..需要最终完成它,因为这里13个小时试着修复它......!

所以......问题..当我用相同的电子邮件注册时,我想再次使用相同的电子邮件注册后得到orm错误...

{
  "code": "E_VALIDATION",
  "invalidAttributes": {
    "email": [
      {
        "value": "mail@mail.com",
        "rule": "unique",
        "message": "A record with that `email` already exists (`lucasgatsas@gmail.com`)."
      }
    ]
  },
  "_e": {},
  "rawStack": "    at WLValidationError.WLError (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/WLError.js:25:15)\n    at new WLValidationError (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/WLValidationError.js:19:28)\n    at duckType (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/index.js:59:12)\n    at errorify (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/index.js:39:10)\n    at wrappedCallback (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:329:15)\n    at error (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:42:31)\n    at _switch (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/switchback/lib/factory.js:56:28)\n    at Timeout._onTimeout (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-disk/lib/adapter.js:179:10)\n    at ontimeout (timers.js:482:11)\n    at tryOnTimeout (timers.js:317:5)\n    at Timer.listOnTimeout (timers.js:277:5)",
  "reason": "1 attribute is invalid",
  "status": 400,
  "details": "Invalid attributes sent to undefined:\n • email\n   • A record with that `email` already exists (`mail@mail.com`).\n",
  "message": "[Error (E_VALIDATION) 1 attribute is invalid] Invalid attributes sent to undefined:\n • email\n   • A record with that `email` already exists (`mail@mail.com`).\n",
  "stack": "Error (E_VALIDATION) :: 1 attribute is invalid\n    at WLValidationError.WLError (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/WLError.js:25:15)\n    at new WLValidationError (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/WLValidationError.js:19:28)\n    at duckType (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/index.js:59:12)\n    at errorify (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/error/index.js:39:10)\n    at wrappedCallback (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/waterline/lib/waterline/utils/normalize.js:329:15)\n    at error (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/switchback/lib/normalize.js:42:31)\n    at _switch (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-hook-orm/node_modules/switchback/lib/factory.js:56:28)\n    at Timeout._onTimeout (/Users/spacedigger/Desktop/passport_with_sails-master/node_modules/sails-disk/lib/adapter.js:179:10)\n    at ontimeout (timers.js:482:11)\n    at tryOnTimeout (timers.js:317:5)\n    at Timer.listOnTimeout (timers.js:277:5)"
}

这是我的user.js文件:

/**
* User.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

var bcrypt = require('bcrypt');

module.exports = {
    attributes: {
        email: {
            type: 'email',
            unique: true,
            required: true
          },
        password: {
            type: 'string',
            minLength: 6,
            required: true
        },
        toJSON: function() {
            var obj = this.toObject();
            delete obj.password;
            return obj;
        }
    },
    beforeCreate: function(user, cb) {
        bcrypt.genSalt(10, function(err, salt) {
            bcrypt.hash(user.password, salt, function(err, hash) {
                if (err) {
                    console.log(err);
                    cb(err);
                } else {
                    user.password = hash;
                    cb();
                }
            });
        });
    }
};

这是我的package.json:

{
  "name": "passport_with_sails",
  "private": true,
  "version": "0.0.0",
  "description": "a Sails application",
  "keywords": [],
  "dependencies": {
    "bcrypt": "^0.8.7",
    "cookie": "file:node_modules/cookie",
    "ejs": "^0.8.8",
    "express": "^4.16.3",
    "grunt": "0.4.2",
    "grunt-sync": "0.0.8",
    "include-all": "^0.1.6",
    "mongodb": "^3.0.5",
    "passport": "^0.2.2",
    "passport-local": "^1.0.0",
    "rc": "^0.5.5",
    "sails": "^0.12.14",
    "sails-disk": "^0.10.10",
    "sails-hook-orm": "^1.0.9",
    "sails-mongo": "^0.12.2",
    "waterline": "^0.13.1"
  },
  "devDependencies": {
    "grunt-contrib-clean": "^0.5.0",
    "grunt-contrib-coffee": "^0.10.1",
    "grunt-contrib-concat": "^0.3.0",
    "grunt-contrib-copy": "^0.5.0",
    "grunt-contrib-cssmin": "^0.9.0",
    "grunt-contrib-jst": "^0.6.0",
    "grunt-contrib-less": "^0.11.1",
    "grunt-contrib-uglify": "^0.4.1",
    "grunt-contrib-watch": "^0.5.3",
    "grunt-sails-linker": "^0.9.6"
  },
  "scripts": {
    "start": "node app.js",
    "debug": "node debug app.js"
  },
  "main": "app.js",
  "repository": {
    "type": "git",
    "url": "git://github.com/balderdashy/sails.git"
  },
  "author": "lucas",
  "license": ""
}

这是passport.js

var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy,
bcrypt = require('bcrypt');

passport.serializeUser(function(user, done) {
    done(null, user.id);
});

passport.deserializeUser(function(id, done) {
    User.findOne({ id: id } , function (err, user) {
        done(err, user);
    });
});

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  },
  function(email, password, done) {

    User.findOne({ email: email }, function (err, user) {
      if (err) { return done(err); }
      if (!user) {
        return done(null, false, { message: 'Incorrect email.' });
      }

      bcrypt.compare(password, user.password, function (err, res) {
          if (!res)
            return done(null, false, {
              message: 'Invalid Password'
            });
          var returnUser = {
            email: user.email,
            createdAt: user.createdAt,
            id: user.id
          };
          return done(null, returnUser, {
            message: 'Logged In Successfully'
          });
        });
    });
  }
));

2 个答案:

答案 0 :(得分:0)

您已使用唯一约束配置User模型的email属性,因此当您尝试使用相同的电子邮件创建另一个时,它将无法通过验证。

答案 1 :(得分:0)

您应首先检查数据库中是否存在emailId,然后才进行插入。您不能在单个查询中检查和插入两者。