-3

i havge some trouble with the sails.js framework. i have some issue with invalidattributes with this waterline and sails.js framework. how ever try this nerving this to fix it. but no change.. i dont know where the problem is... and its very crash down... is the error in this script? are someone one here which can tell me and explain to me what i can do, thats this creepy error with the waterline can stop it.? there is no support in the sails.js - however.. need to finish it finally since 13 hours here try to fix it... !

so.. the problem.. when i do signup with the same email i get the orm error back after i wanted to signup with same e-mail again...

{
  "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)"
}

and this is my user.js file :

/**
* 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();
                }
            });
        });
    }
};

and this is my 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": ""
}

and this are 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'
          });
        });
    });
  }
));
Pueblo
  • 1
  • 2

2 Answers2

0

You've configured the email property of the User model with a unique constraint, so that when you attempt to create another with the same email, it fails the validation.

ThisIsNoZaku
  • 2,213
  • 2
  • 26
  • 37
  • yes, of corse. i want a message back with the message "this user with the email is allready registed".-... and not a error.. i want clean stuff and not so fake things! clean development...!!!!!! – Pueblo Mar 25 '18 at 14:26
  • How are you trying to create the user? I assume you're making a POST request against the User endpoint. Nothing in the code you've shown looks like it will ever run in the default case except your beforeCreate hook, which won't matter for this error. – ThisIsNoZaku Mar 25 '18 at 14:37
  • with a sigunp form? with a the sigunp page?!... - no, i have created 2 pages, signup and login...! in the sign up page with a form- the login form too with a form... - – Pueblo Mar 25 '18 at 14:40
  • Ok, but your front end needs to tell your server to create a new user. How is that communication happening? – ThisIsNoZaku Mar 25 '18 at 14:42
  • its in the form the action method POST - – Pueblo Mar 25 '18 at 15:10
  • Assuming you're using the Sails blueprints, Sails creates default implementations of the standard actions for your model. This handler is being called when your form is POSTing to your server and it automatically goes to the ORM to create your entity. Nowhere in that process is any of the code you provided executed, you'll have to override the create action or find some other way to either check if the entity exists before saving it or handle when the error happens. – ThisIsNoZaku Mar 25 '18 at 15:37
  • but, one question, this is a error? you want see the repo ? and tell me maybe where the error is.? .. would be glad for it,.. – Pueblo Mar 25 '18 at 17:59
  • @ThisIsNoZaku: please check out [help vampire](https://meta.stackoverflow.com/questions/258206/what-is-a-help-vampire). – Hovercraft Full Of Eels Mar 25 '18 at 19:53
0

You should check for the existence of emailId in your DB first then only do insert. You can't do check and insert both in single query.

krishnashu
  • 42
  • 5