Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model instance methods are no longer supported in Sails v1

I keep getting the error "In the customToJSON attribute of model user: Model instance methods are no longer supported in Sails v1. Please refactor the logic from this instance method into a static method, model method or helper.". I'm new to node.js and don't know what do to about it.

My User.js

const bcrypt = require("bcrypt")
const Promise = require("bluebird")

module.exports = {
  attributes: {
    firstName: {
      type: "string",
      required: true
    },
    lastName: {
      type: "string",
      required: true
    },
    username: {
      type: "string",
      required: true,
      unique: true
    },
    email: {
      type: "email",
      required: true,
      unique: true
    },
    password:{
      type: "string",
      minLength: 6,
      required: true,
      columnName: "encryptedPassword"
    },  
    customToJSON: function(){
        const obj = this.toObject()
        delete obj.password
    }
    }
};
like image 586
razvanusc Avatar asked Mar 04 '23 11:03

razvanusc


1 Answers

Firstly, I hope you are enjoying developing with Node and Sails.

Now to solve your issue, I guess you are using a guide or tutorial based off sails v0.12 but like the error message says, instance methods have been removed from Sails and Waterline as of v1.

With that said, the solution is quite an easy one, move your customToJSON method out of the attributes section of your model. This basically allows sails v1 to find it.

So your model will look something like this

...
attributes: {
    ...
    ,
    password:{
      type: "string",
      minLength: 6,
      required: true,
      columnName: "encryptedPassword"
    },
},

customToJSON: function() {
    return _.omit(this, ['password']); 
},
...

For more info on customToJSON in sails see here and for more info on replacing instance methods see here.

like image 57
Glen Avatar answered Mar 16 '23 17:03

Glen