2
votes

I can initialize a waterline model using following way.

    var user = Waterline.Collection.extend({

      attributes: {
        name: 'string',
        password: 'string',

        // Override toJSON instance method 
        toJSON: function() {
          var obj = this.toObject();
          delete obj.password;
          return obj;
        }
      }
    });
module.exports = user;

Then I tried to instantiated a waterline model in a different file using following method.

var user = require('./user'); user.find({ id: 1 }).exec(function(err, model) { return model.toJSON(); // Will return only the name });

But then I get the following error.

User.find({ "first_name": "lahiru" }).exec(function (err, model) {
     ^
TypeError: undefined is not a function

Is there a specific way to instantiated a user model from the initialized user. Any kind of help would be appreciated.

1

1 Answers

0
votes

You have to instantiate Waterline

var Waterline = require('waterline');
var waterline= new Waterline();

Then you have to load the collection

var User = require('user.js');
waterline.loadCollection(User);

Finally initialize Waterline

waterline.initialize(connectionConfig, function(err, models) {
   if(err) {
         logger.err('Error initializing ORM', err);
         throw err;
   };
   models.collections.user.find().exec(function(err, users){
       if(err) throw err;
       console.log(users);
   })
});

You will find your models under models.collections. Nb : give an identity name to your model

var user = Waterline.Collection.extend({
  identity : 'user',
  attributes: {
    name: 'string',
    password: 'string',

    // Override toJSON instance method 
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      return obj;
    }
  }
});