0

I am trying to check if the record exists inside the model using beforeCreate

/api/models/User.js
  beforeCreate: function (values, cb) {
    User.findOne(values.email).exec(function findOneCB(err,found){
         console.log('We found '+found.name);
    });
  },

I get

TypeError: Object #<Object> has no method 'findOne'

Is there a way to avoid doing that from the controller?

Gadelkareem
  • 1,067
  • 13
  • 30

2 Answers2

2

You can set validation for email as unique.

module.exports = {

  attributes: {

    email: {
        type: "email",
        required: true,
        unique: true
    },

    name: {
        type: "string",
        required: true
    } 
  },
};

Or if you want to do this in beforeCreate, then you need to provide the matching condition properly. See this:

module.exports = {

  attributes: {

    email: {
        type: "email",
        required: true
    },

    name: {
        type: "string",
        required: true
    } 
  },

  beforeCreate: function (values, cb) {
    User.findOne({'email': values.email})
    .exec(function (err,found){
        if(found){
                console.log('We found '+found.name);
                cb({"error": "duplicate entry"});           
        }
        else{
            cb(err, found);
        }
    });
  },

};
Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104
  • my DB does not support unique indexes. For the example you posted still the User model is not defined – Gadelkareem Dec 01 '14 at 18:00
  • beforeCreate executes before creation of an instance of user. User model is defined and created as and when you lift sails and you can access User model in beforeCreate. The code that I have posted, I have tested it personally and it worked perfectly – Mandeep Singh Dec 01 '14 at 18:12
  • I keep getting User object is null. I think what you said about creating the User model is later after executing beforeCreate so there is no way to access User from within the model – Gadelkareem Dec 01 '14 at 18:50
  • You are confusing the concept here. User model is the blueprint. The purpose is to create the table structure in the database. However, beforeCreate is used when you create an instance of User, i.e. inserting a row in the user table. If you are getting error, it must be due to something else. Could you post the error message and also let me know which DB are you using ? Contents of config/connections.js will be useful too – Mandeep Singh Dec 01 '14 at 18:54
  • Well I will try to retest it then and get back to you. Thanks a lot! – Gadelkareem Dec 01 '14 at 19:04
  • just tested again and it has the same error ex: beforeCreate: function(){console.log(User.findOne(1));} Creates error: TypeError: Object # has no method 'findOne' – Gadelkareem Dec 02 '14 at 10:07
  • Please check your findOne. You need to specify the match condition like a JSON object findOne({'id': 1}). Also, what is your underlying DB ? – Mandeep Singh Dec 02 '14 at 10:18
  • The same code would run outside this function. I use DynamoDB – Gadelkareem Dec 02 '14 at 11:39
  • then it seems like a problem in your database adapter. I have tested my code with sails-disk, mongodb, postgres and mysql. It works like a charm on all those databases – Mandeep Singh Dec 02 '14 at 11:42
  • @gadelkareem, try to use `sails.models['user'].findOne(...` when you need to perform a query on the model from within the file that contains the model definition, it worked for me. – Leonardo Nov 24 '15 at 09:31
2

Solved by using

sails.models['user']

instead of User

Gadelkareem
  • 1,067
  • 13
  • 30