Sails: toJson is not working?

Created on 18 Oct 2018  路  9Comments  路  Source: balderdashy/sails

Sails version: 1.0.2
Node version: 8.12.0
NPM version: 6.4.1
DB adapter name: sails-mysql
DB adapter version: N/A
Operating system: windows 10



Hello,
I am using sails version 1.0.2. It encounted an error when i add my toJson function in model. I have already searched to fix this issue. But i don't find any solution. And i am very new to nodejs and sailsjs. Here is the code:

module.exports = {

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

      email:{
        type: "string", 
//        email: true,
        required: true,
        unique: true
      },
      encryptedPassword:{
        type: "string",  
      },

      toJson: function(){
          var obj = this.toObject();
          delete obj.password;
          delete obj.confirmation;
          delete obj.encryptedPassword;
          delete obj._csrf;
          return obj;
      }
  }
};

In the toJson attribute of model users:
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.

mysql orm resolved

Most helpful comment

@thg303: Thanks for the help, I was using arrow functions and it was causing all the troubles.

My working solution -

module.exports = {
  attributes: {
    ...
  },
  customToJSON: function() {
    return _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']);
  },
  // This won't work -
  // customToJSON: () => _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']),
};

SO: Arrow functions and this

All 9 comments

Hi @devkbsc! It looks like you missed a step or two when you created your issue. Please edit your comment (use the pencil icon at the top-right corner of the comment box) and fix the following:

  • Verify "I am experiencing a concrete technical issue (aka a bug) with Sails (ideas and feature proposals should follow the guide for proposing features and enhancements (http://bit.ly/sails-feature-guide), which involves making a pull request). If you're not 100% certain whether it's a bug or not, that's okay--you may continue. The worst that can happen is that the issue will be closed and we'll point you in the right direction."
  • Verify "I am not asking a question about how to use Sails or about whether or not Sails has a certain feature (please refer to the documentation(http://sailsjs.com), or post on http://stackoverflow.com, our Google Group (http://bit.ly/sails-google-group) or our live chat (https://gitter.im/balderdashy/sails)."
  • Verify "I have already searched for related issues, and found none open (if you found a related _closed_ issue, please link to it in your post)."
  • Verify "My issue title is concise, on-topic and polite ("jst.js being removed from layout.ejs on lift" is good; "templates dont work" or "why is sails dumb" are not so good)."
  • Verify "I have tried all the following (if relevant) and my issue remains:"
  • Verify "I can provide steps to reproduce this issue that others can follow."

As soon as those items are rectified, post a new comment (e.g. “Ok, fixed!”) below and we'll take a look. Thanks!

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]

The function I'm guessing you're trying to implement should be added as a property to the 'module.exports' object named 'customToJSON' (instead of 'toJson'). In you're code you're actually trying to define it in the same object as your attributes (/ columns). So you're code should probably look like this:

module.exports = {
  attributes: {
      name:{
        type: "string",  
        required: true
      },
      title:{
        type: "string",  
      },
      email:{
        type: "string", 
        //email: true,
        required: true,
        unique: true
      },
      encryptedPassword:{
        type: "string",  
      }
  },
  customToJSON: function(){
      var obj = this.toObject();
      delete obj.password;
      delete obj.confirmation;
      delete obj.encryptedPassword;
      delete obj._csrf;
      return obj;
  }
};

Official documentation


In the customToJSON attribute of model users:
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.


still i am getting the same error! Did i miss to import any extentions? sorry i am new to sails! so i started my practice project.

Sorry to be a hassle, but it looks like your issue is still missing some required info. Please double-check your initial comment and try again.

*If you feel this message is in error, or you want to debate the merits of my existence (sniffle), please contact [email protected]

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_- In the customToJSON attribute of model users: 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. -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

still i am getting the same error! Did i miss to import any extentions? sorry i am new to sails! so i started my practice project.

The error makes it seem like you still define 'customToJSON' inside the 'attributes' of your model. Make sure you define it outside of the attributes.

module.exports = {
  attributes: {
    // ... your attributes
  },
  // Now it is outside of your attributes
  customToJSON: function(){
      var obj = this.toObject();
      delete obj.password;
      delete obj.confirmation;
      delete obj.encryptedPassword;
      delete obj._csrf;
      return obj;
  }
};

Hi @nino-vrijman thanks for helping out a fellow sailor!
@devkbsc Are you still experiencing this problem?

here I am using "1.1.0" and the problem still exist.
when I tried it, in console this is a plain object {} and this.toObject() is not a function.
I figured it out, it's because of using arrow functions. I think this could save some time to mention this in the docs.

@thg303: Thanks for the help, I was using arrow functions and it was causing all the troubles.

My working solution -

module.exports = {
  attributes: {
    ...
  },
  customToJSON: function() {
    return _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']);
  },
  // This won't work -
  // customToJSON: () => _.omit(this, ['password', 'confirmation', 'encryptedPassword', '_csrf']),
};

SO: Arrow functions and this

customToJSON is not even calling for me, created an issue - https://github.com/balderdashy/sails/issues/6865

Was this page helpful?
0 / 5 - 0 ratings