Stanislav
Stanislav

Reputation: 516

Creating new record in database (Sails.js)

I created model Pattern.js, looks this way:

var Pattern = {

  adapter: 'land',

  attributes: {
    name: 'STRING',
    code: 'STRING'
  }
};

module.exports = Pattern;

And now I have two problems:

  1. I can't get access from my HomeController to this model. I tried to require it, but code fails. How to get access to the models from other files?
  2. There is no method 'create' in the documentation, so what is the way to create new record using a defined model?

Upvotes: 0

Views: 6136

Answers (1)

marionebl
marionebl

Reputation: 3382

Accessing models

You do not have to require the model yourself, Sails does this for you.

Every model you define is available in the global scope as Filename (uppercase first letter) and in sails.models.filename (all lowercase) by default after the orm hook has run.

sails is available in the global scope too.

Documentation

I do not know wich docs you refer to, but there is a create method available on Sails / Waterline models. In your example:

Pattern.create(data).exec(callback);

You can look up on Sails / Waterline models here:

Upvotes: 2

Related Questions